简体   繁体   English

JavaScript 将对象数组映射到新对象数组的最短方法?

[英]JavaScript shortest way to map an array of objects to a new array of new objects?

I have an array of person objects, where each person have an array of profiles objects which consist of name and an image url, and a list of addresses objects which consist of lat and long properties, as follows:我有一个person对象数组,其中每个person都有一个由 name 和 image url 组成的profiles对象数组,以及一个由latlong属性组成的addresses 对象列表,如下所示:

var listOfPersons = [{
    addresses : [{lat:11, long:11}, {lat:22, long:22}],
    profile: [{image:"some_url1", name: "peter parker"}]
},
{
    addresses : [{lat:33, long:33}, {lat:44, long:44}],
    profile: [{image:"some_url2", name: "bruce wayne"}]
}];

I need to create a new array of objects, where each new object has an image , long , lat properties, for each set of lat long , as follows:我需要创建一个新的对象数组,其中每个新对象对于每组lat long都有一个imagelonglat属性,如下所示:

var expectedResult = [
{
    image:"some_url1",
  lat:11,
  long:11
},
{
    image:"some_url1",
  lat:22,
  long:22
},
{
    image:"some_url1",
  lat:33,
  long:33
},
{
    image:"some_url1",
  lat:44,
  long:44
}
];

What is the shortest way (in terms of writing code) to map \\ reduce the first array into the second?什么是最简单的办法(在编写代码的条款),以map \\ reduce第一阵列进入第二?

You can use nested Array.flatMap() with Array.map() to iterate the array/address/profiles, and combine image and lat , long properties into a single object:您可以使用嵌套的Array.flatMap()Array.map()来迭代数组/地址/配置文件,并将imagelat , long属性组合到一个对象中:

 const listOfPersons = [{"addresses":[{"lat":11,"long":11},{"lat":22,"long":22}],"profile":[{"image":"some_url1","name":"peter parker"}]},{"addresses":[{"lat":33,"long":33},{"lat":44,"long":44}],"profile":[{"image":"some_url2","name":"bruce wayne"}]}]; const result = listOfPersons.flatMap(o => o.addresses.flatMap(({ lat, long }) => o.profile.map(({ image }) => ({ image, lat, long })) ) ); console.log(result);

If you always use just the 1st profile always, you can remove one level of Array.flatMap() :如果您始终只使用第一个配置文件,则可以删除一层Array.flatMap()

 const listOfPersons = [{"addresses":[{"lat":11,"long":11},{"lat":22,"long":22}],"profile":[{"image":"some_url1","name":"peter parker"}]},{"addresses":[{"lat":33,"long":33},{"lat":44,"long":44}],"profile":[{"image":"some_url2","name":"bruce wayne"}]}]; const result = listOfPersons.flatMap(o => o.addresses.map(({ lat, long }) => ({ image: o.profile[0].image, lat, long })) ); console.log(result);

You can use Array.prototype.reduce() with combined Array.prototype.forEach() .您可以将Array.prototype.reduce()Array.prototype.forEach()结合使用。

The documentation states for reduce() : reduce()的文档说明:

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value. reduce() 方法在数组的每个元素上执行一个 reducer 函数(您提供的),从而产生单个输出值。

I think the following can work for you:我认为以下方法对您有用:

 const listOfPersons = [{ addresses : [{lat:11, long:11}, {lat:22, long:22}], profile: [{image:"some_url1", name: "peter parker"}] }, { addresses : [{lat:33, long:33}, {lat:44, long:44}], profile: [{image:"some_url2", name: "bruce wayne"}] }]; const result = listOfPersons.reduce((acc, cur) => { cur.addresses.forEach(e => acc.push({ ...e, image: cur.profile[0].image })); return acc; }, []); console.log(result);

I hope that helps!我希望这有帮助!

Sinced you asked for shortest in writing code:由于您要求编写代码最短:

 var listOfPersons = [{addresses: [{lat:11, long:11}, {lat:22, long:22}],profile: [{image:"some_url1", name: "peter parker"}]},{addresses:lat:33, long:33}, {lat:44, long:44}],profile: [{image:"some_url2", name: "bruce wayne"}]}]; const res = listOfPersons.reduce((r,{addresses:a,profile:[{image}]})=> [...r,...a.map(o=>({image,...o}))],[]); console.log(res);

Here's with formatting and better variable names:这是格式和更好的变量名称:

 var listOfPersons = [{ addresses : [{lat:11, long:11}, {lat:22, long:22}], profile: [{image:"some_url1", name: "peter parker"}] }, { addresses : [{lat:33, long:33}, {lat:44, long:44}], profile: [{image:"some_url2", name: "bruce wayne"}] }]; const res = listOfPersons.reduce((acc, { addresses: adr, profile: [{image}] }) => [...acc, ...adr.map(a => ({image, ...a}) )], []); console.log(res);

var listOfPersons = [
  {
    addresses: [{ lat: 11, long: 11 }, { lat: 22, long: 22 }],
    profile: [{ image: "some_url1", name: "peter parker" }]
  },
  {
    addresses: [{ lat: 33, long: 33 }, { lat: 44, long: 44 }],
    profile: [{ image: "some_url2", name: "bruce wayne" }]
  }
];

var expectedResult = listOfPersons.reduce(
  (acc, person) => ([
    ...acc,
    ...person.addresses.map(
      address => ({ ...address, image: person.profile[0].image })
    )
  ]),
  []
)

This will give you what you want assuming you always want the first result from .profile假设您总是想要.profile的第一个结果,这将为您提供所需的内容

var listOfPersons = [{
    addresses : [{lat:11, long:11}, {lat:22, long:22}],
    profile: [{image:"some_url1", name: "peter parker"}]
},
{
    addresses : [{lat:33, long:33}, {lat:44, long:44}],
    profile: [{image:"some_url2", name: "bruce wayne"}]
}];


var expectedResult = [];
expectedResult.forEach(person => {
    person.addresses.forEach(address => expectedResult.push({image: person.profile[0].image, ...address}))
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 重新格式化或 map 将对象数组转换为新对象数组? 就像重新格式化它并为值提供新的键(名称) - Javascript reformat or map an array of objects to a new array of new objects? like reformating it and giving new keys (names) for values 将对象数组修改为 JavaScript 中的新对象数组 - Modify array of objects into new array of objects in JavaScript Map 将对象数组放入新组 - Map array of objects into new groups Javascript 重新格式化或 map 将对象数组转换为新对象数组? 就像重新改造它 - Javascript reformat or map an array of objects to a new array of new objects? like reformating it 如何将 map 对象数组转换为新数组 - How to map an array of objects to a new array 比较2个对象以在javascript中创建对象的新数组 - Compare 2 objects to create a new array of objects in javascript 使用 map JavaScript 创建一个新的对象文档数组 - Create a new array of documents of objects using map JavaScript Map 通过 Javascript 对象数组并返回一个满足条件的新对象 - Map through a Javascript array of objects and return a new one satisfying a condition 从Javascript中的数组创建新的对象数组 - Creating a new array of objects from an array in Javascript 如何 map 具有新键值的新数组中的对象数组 - How to map an array of objects in a a new array with new key value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM