2π
β
let arr1 = [1, 1, 1];
let arr2 = [2, 2, 2];
let reqArray = [arr1, arr2].map((ele, ind) => {
return {
data: ele,
label: `some label${ind+1}`
}
});
console.log(reqArray)
0π
By using the list of below statements you can achieve the above array of objects.
var array1 = [1,1,1];
var array2 = [2,2,2];
var arrObj = [];
var tempObj = {};
tempObj.data = array1;
tempObj.lable = "test1";
arrObj.push(tempObj);
tempObj.data = array2;
tempObj.lable = "test1";
arrObj.push(tempObj);
βarrObjβ has the sample output that you wanted
0π
use map and spread operator
const array1 = [1,1,1];
const array2 = [2,2,2];
const parentArr = [
{ label: 'some label1' },
{ label: 'some label2' },
]
function a (array) {
return parentArr1 = parentArr.map(it => {
return {...it, data: array }
})
}
console.log(a(array1))
console.log(a(array2))
Source:stackexchange.com