How to create array of object from tow arrays with extra properties?

Issue

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.

Solution

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);

Answered By – Mamun

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published