Home > Blockchain >  How to create array of object from tow arrays with extra properties? [duplicate]
How to create array of object from tow arrays with extra properties? [duplicate]

Time:09-23

I need help to create an array of objects from two exiting array

["A","B","C"]
[1,2,3] 

What I want is something like the below:

[{name: "A", id: 1, trash: false},{name: "B", id: 2, trash: false},{name: "C", id: 3, trash: false}]

properties should be defined as I want it like name and id and the value of them should come from existing arrays.

CodePudding user response:

Hope this code helping you

const array = ["A","B","C"]
const num = [1,2,3] 
array.map((o,index)=>({name: o,id:num[index],trash:false}))

CodePudding user response:

You can try using Array.prototype.map() that creates a new array populated with the results of calling a provided function on every element in the calling array.

var arr1 = ["A","B","C"]
var arr2 = [1,2,3] 
var resArr = arr1.map((i,idx) => ({name: i, id: arr2[idx], trash: false}));
console.log(resArr);

  • Related