![](/img/trans.png)
[英]How can I merge value from one object to an element in an array of objects?
[英]How can I merge different array objects based on one element
如何合並這兩個 arrays?
array1 = [
{ image: 'image1', title: 'title1' },
{ image: 'image2', title: 'title2' },
{ image: 'image3', title: 'title3' },
];
array2 = [
{ downloadLink: 'downloadLink1', fileName: 'fileName1' },
{ downloadLink: 'downloadLink2', fileName: 'fileName2' },
{ downloadLink: 'downloadLink3', fileName: 'fileName3' },
];
我想得到以下結果:
array3 = [
{ image: 'image1', downloadLink: 'downloadLink1', fileName: 'fileName1' },
{ image: 'image2', downloadLink: 'downloadLink2', fileName: 'fileName2' },
{ image: 'image3', downloadLink: 'downloadLink3', fileName: 'fileName3' },
];
你可以簡單地試試這個:
const array1 = [ { image: 'image1', title: 'title1' }, { image: 'image2', title: 'title2' }, { image: 'image3', title: 'title3' }, ]; const array2 = [ { downloadLink: 'downloadLink1', fileName: 'fileName1' }, { downloadLink: 'downloadLink2', fileName: 'fileName2' }, { downloadLink: 'downloadLink3', fileName: 'fileName3' }, ]; let result = [] array1.forEach(({image},index) => { result[index] = {image, ...array2[index]} }) console.log(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.