I have an array of strings
[ '/nft-card', '/landing' ]
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?
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
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)
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.