简体   繁体   English

合并JavaScript中相同长度的多个对象数组

[英]Merging multiple arrays of objects of the same length in JavaScript

Here are examples of the arrays that I need to merge 这是我需要合并的数组的示例

const urls = [{url:"http:/..."},{url:"http:/..."},{url:"http:/..."}]; 
const images = [{image:"..."},{image:"..."},{image:"..."}];

const merged = [{url:"http:/...", image:"..."},{url:"http:/...", image:"..."},{url:"http:/...", image:"..."}]

I have tried Object.assign({},urls, images). 我已经尝试过Object.assign({},urls,images)。 That ends up just deleting urls in that instance because it does not deep copy. 最终只能删除该实例中的url,因为它不会进行深度复制。 The keys for each object in the array are the same! 数组中每个对象的键都相同!

If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method. 如果您确定它们大小相等,则可以使用Array.prototype.map方法遍历其中之一。 Actually, you should use Object.assign if you want to merge an object with a more generic way. 实际上,如果要以更通用的方式合并对象,则应使用Object.assign

 const urls = [{url:"http:/..."},{url:"http:/..."},{url:"http:/..."}]; const images = [{image:"..."},{image:"..."},{image:"..."}]; const results = urls.map((url, index) => Object.assign({}, url, images[index]) ); console.log(results) 

ES6 ES6

you can use ES6 instead of Object.assign. 您可以使用ES6代替Object.assign。

const results = urls.map((url, index) => 
  ({...url, ...images[index]})
);

You could map over one of the arrays and use the index to get the other array's item: 您可以map一个数组,并使用索引获取另一个数组的项:

 const urls = [{url:"http:/0..."},{url:"http:/1..."},{url:"http:/2..."}]; const images = [{image:"0..."},{image:"1..."},{image:"2..."}]; const newArray = urls.map((url, i) => ({...url, ...images[i] }) ) console.log(newArray) 

Or you could use Array.from like this: 或者您可以像这样使用Array.from

 const urls = [{url:"http:/0..."},{url:"http:/1..."},{url:"http:/2..."}], images = [{image:"0..."},{image:"1..."},{image:"2..."}]; const length = urls.length const newArray = Array.from({ length }, (_, i) => ({ ...urls[i], ...images[i] }) ) console.log(newArray) 

You could iterate over one of the arrays using Array.prototype.map , and using the index provided to the callback function, you can get corresponding value in the other array. 您可以使用Array.prototype.map遍历一个数组,并使用提供给回调函数的索引,在另一个数组中获取相应的值。

Here's an example: 这是一个例子:

 const urls = [{url:"http:/..."},{url:"http:/..."},{url:"http:/..."}]; const images = [{image:"..."},{image:"..."},{image:"..."}]; console.log(urls.map((t, id) => ({ image: images[id].image, url: t.url }))); 

Here the parameters passed into the callback function are t : the item from urls array and id : index of t in urls array. 在这里,传递给回调函数的参数是turls数组中的项, idturls数组中的索引。

You could reduce the arrays and assign the object to the object with the same index. 您可以减少数组并将对象分配给具有相同索引的对象。

This works for an arbitrary count of arrays. 这适用于任意数量的数组。

 const urls = [{url:"http:/..."}, { url: "http:/..." }, { url: "http:/..." }], images = [{ image: "..." }, { image: "..." }, { image: "..." }], result = [urls, images] .reduce((r, a) => ( a.forEach((o, i) => Object.assign(r[i] = r[i] || {}, o)), r ), []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM