Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 25, 2022 05:30
Show Gist options
  • Save thmain/12541fd314e21c889d152b752f378a9c to your computer and use it in GitHub Desktop.
Save thmain/12541fd314e21c889d152b752f378a9c to your computer and use it in GitHub Desktop.
'use strict';
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
if (Array.isArray(subArray))
subArray.forEach((item) => results.push(item))
else
throw new Error('Its not two dimensional array;');
});
return results;
};
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 = [];
return movieLists.map(movieList =>
movieList.videos.map(video =>
video.id
) // [id, id]
) // [[id, id], [id, id]]
.concatAll(); // [id, id, id, id]
}
console.log(queryNestedArray()); // [70111470, 654356453, 65432445, 675465]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment