简体   繁体   English

如何从JSON返回嵌套在对象中的嵌套JavaScript数组的属性值?

[英]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.

相关问题 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何从 JavaScript 中的嵌套数组对象中提取特定属性作为数组 - How can I extract specific property as an array from nested array object in JavaScript 如何从 Javascript 中的嵌套对象获取值数组 - How do I get array of values from a nested object in Javascript 如何使用JavaScript在嵌套对象数组中按属性分配值 - How to assign values by property in nested object array using javascript 如何遍历嵌套对象并返回数组中的值? - How do I Iterate through a nested object and return values in an array? 通过 object 属性返回嵌套数组 - Return nested array by object property 在 javascript 如何动态获取 object 的嵌套属性 - In javascript how can I dynamically get a nested property of an object 如何从嵌套数组 JavaScript/React 返回数组 - How do i return an Array from a nested Array JavaScript/React 如何从嵌套的 JSON 对象中获取检查值数组 - How to get array of checked Values from the nested JSON Object 如何从JavaScript访问JSON对象内的嵌套数组 - How to access nested array inside a JSON object from javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM