[Fixed] Best approach in moving data from one array to a new array in angular ts

Issue

.subscribe((dataChart) => {
    // console.log(dataChart)
    var forxaxis = []
    
    var cd = [dataChart]
    // console.log(cd)
    cd.forEach(element => {
      forxaxis.push(element.strRequestDate)
      console.log(forxaxis)
    });
    },

Im trying to move my data in the first array into a new array so that I can use it with chart.js. but it didnt work.

dataChart contain 2 column of data. i insert dataChart into an array called cd. then i tried to push one of the column from dataChart which is called strRequestDate into a new array called forxaxis but it just didnt work as per expected. the result is as shown in the image attached.

this is how the data look like. it was called by using sharepoint API

error and the data

Solution

You can use array.map property here, so you don’t need to push data manually from one array to another

I have taken sample data in dataChart array for demonstration purpose only.

let dataChart = [{strRequestId: 1, strRequestDate: 'ABC'}, {strRequestId: 1, strRequestDate: 'PQR'}];

let forxaxis = dataChart.map(x => x.strRequestDate);

console.log(forxaxis);

Demo

Output:

enter image description here

Leave a Reply

(*) Required, Your email will not be published