DEV Community

Mr.Morsalin
Mr.Morsalin

Posted on

style={{whiteSpace:"pre-line"}}

Querying Nested Arrays

To work with tree data structure we need to flatten them.

We will be solving a problem using Array.prototype.forEach().
We will define Array.prototype.concatAll() using Array.prototype.forEach().
We will solve the same problem using Array.prototype.concatAll()

This post is a follow up post of the
– Getting started Functional Programming in JavaScript. I would recommend reading that post first.
Enter fullscreen mode Exit fullscreen mode

Problem

Flatten the movieLists array into an array of video ids

Let’s solve with Array.prototype.forEach()
Enter fullscreen mode Exit fullscreen mode

'use strict';

function queryNestedArray() {
var movieLists = [{
name: "New Releases",
videos: [{
"id": 70111470,
"title": "Die Hard",
"rating": 4.0
}, {
"id": 654356453,
"title": "Bad Boys",
"rating": 5.0
}]
}, {
name: "Dramas",
videos: [{
"id": 65432445,
"title": "The Chamber",
"rating": 4.0
}, {
"id": 675465,
"title": "Fracture",
"rating": 5.0
}]
}],
allVideoIdsInMovieLists = [];

movieLists.forEach(function(movieList) {
return movieList.videos.forEach(function(video) {
return allVideoIdsInMovieLists.push(video.id);
});
});

return allVideoIdsInMovieLists;
}

console.log(queryNestedArray()); // [70111470, 654356453, 65432445, 675465]

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more