I have an array of strings
[ '/landing', '/nft-card' ]
I want this array as a property in this object array
[{ id: 1, name: 'NFT Preview Card', },{ id: 2, name: 'Remote Landing Page' }]
Result should be like this
[{ id: 1, name: 'NFT Preview Card', link: "/nft-card },{ id: 2, name: 'Remote Landing Page', link: "/landing" }]
How to do this in javascript?
CodePudding user response:
Assuming both arrays are ordered correctly this can be achieved likes so.
const links = ['/nft-card', '/landing'] // changed the order so the result is what you want
const incompleteObjects = [{ id: 1, name: 'NFT Preview Card', },{ id: 2, name: 'Remote Landing Page' }]
const completeObjects = incompleteObjects.map((item, idx) =>
{
return {
...item,
links: links[idx]
}
}
EDIT: adjusted links
array order
CodePudding user response:
You can make copy of array 2. This is easy way to do:
let ar1=[ '/landing', '/nft-card' ]
let ar2=[{ id: 1, name: 'NFT Preview Card', },{ id: 2, name: 'Remote Landing Page' }]
let ar3=[]
for(let i=0;i<ar2.length;i ){
ar2[i].line=ar1[i];
}
console.log(ar2)
CodePudding user response:
if the length of your array and your object array is equal you can use this way:
let array = [ '/landing', '/nft-card' ];
let objectArray = [{ id: 1, name: 'NFT Preview Card', link: "nft-card" },{ id: 2, name: 'Remote Landng Page', link: "landing" }];
for(let i = 0 ; i < array.length; i ){
objectArray[i].link = array[i];
}
console.log(objectArray);