[英]How can I return the property values of a nested JavaScript array nested in an object from JSON?
Given a JS array containing many objects which all contain arrays: 给定一个JS数组,其中包含许多都包含数组的对象:
var data = [
{id: 1, name: "Fred", pages:[{url:"www.abc.com", title: "abc"}]},
{id: 2, name: "Wilma", pages:[{url:"www.123.com", title: "123"}]},
{id: 3, name: "Pebbles", pages:[{url:"www.xyz.com", title: "xyz"}]}
];
How do I efficiently extract the inner most array (pages) values into an array? 如何有效地将最里面的数组(页面)值提取到数组中?
var dataArray = [
{url: "www.abc.com", title: "abc"},
{url: "www.123.com", title: "123"},
{url: "www.xyz.com", title: "xyz"}
]
The easiest way to do this is to use Array#map
like so: 最简单的方法是像这样使用Array#map
:
var dataArray = data.map(function(o){return o.pages});
If pages
is an array of objects (not a single object), this will result in an array of arrays, which you will need to flatten out for example using Array#reduce
: 如果pages
是对象数组(而不是单个对象),则将导致数组数组,例如,您需要使用Array#reduce
进行展平:
dataArray = dataArray.reduce(function(a,b){return a.concat(b)}, []);
You are looking for a flatMap
您正在寻找一个flatMap
var data = [ {id: 1, name: "Fred", pages:[{url:"www.abc.com", title: "abc"}]}, {id: 2, name: "Wilma", pages:[{url:"www.123.com", title: "123"}]}, {id: 3, name: "Pebbles", pages:[{url:"www.xyz.com", title: "xyz"}]} ]; const concat = (xs, ys) => xs.concat(ys); const prop = x => y => y[x]; const flatMap = (f, xs) => xs.map(f).reduce(concat, []); console.log( flatMap(prop('pages'), data) );
Here's a working example on how to achieve what you want: 这是有关如何实现所需目标的有效示例:
var data = [
{id: 1, name: "Fred", pages:[{url:"www.abc.com", title: "abc"}, {url:"www.google.com", title: "Google"}]},
{id: 2, name: "Wilma", pages:[{url:"www.123.com", title: "123"}]},
{id: 3, name: "Pebbles", pages:[{url:"www.xyz.com", title: "xyz"}]}
];
var arr = Array();
var arr2 = Array();
// You can either iterate it like this:
for (var i = 0; i < data.length; i++) {
// If you only want the first page in your result, do:
// arr.push(data[i].pages[0]);
// If you want all pages in your result, you can iterate the pages too:
for (var a = 0; a < data[i].pages.length; a++) {
arr.push(data[i].pages[a]);
}
}
// Or use the array map method as suggested dtkaias
// (careful: will only work on arrays, not objects!)
//arr2 = data.map(function (o) { return o.pages[0]; });
// Or, for all pages in the array:
arr2 = [].concat(...data.map(function (o) { return o.pages; }));
console.log(arr);
console.log(arr2);
// Returns 2x [Object { url="www.abc.com", title="abc"}, Object { url="www.123.com", title="123"}, Object { url="www.xyz.com", title="xyz"}]
If by "efficiently" you actually mean "concisely", then 如果“有效”实际上是“简洁”的意思,那么
[].concat(...data.map(elt => elt.pages))
The data.map
will result in an array of pages
arrays. data.map
将导致pages
数组的数组。 The [].concat(...
then passes all the pages
arrays as parameters to concat
, which will combine all of their elements into a single array. [].concat(...
然后将所有pages
数组作为参数传递给concat
,它将把它们的所有元素组合到一个数组中。
If you are programming in ES5, the equivalent would be 如果您在ES5中编程,则等效为
Array.prototype.concat.apply([], data.map(function(elt) { return elt.pages; }))
use array map() & reduce() method : 使用数组map()和reduce()方法:
var data = [ {id: 1, name: "Fred", pages:[{url:"www.abc.com", title: "abc"}]}, {id: 2, name: "Wilma", pages:[{url:"www.123.com", title: "123"}]}, {id: 3, name: "Pebbles", pages:[{url:"www.xyz.com", title: "xyz"}]} ]; var dataArray = data.map(function(item) { return item.pages; }); dataArray = dataArray.reduce(function(a,b) { return a.concat(b); }, []); console.log(dataArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.