Chartjs-How to insert arrays into objects that are inside an array? For ng2-charts / Charts.js

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

Leave a comment