Chartjs-How can i loop some specific key in each index json data and put it in dataset: data ? (chart.js)

1๐Ÿ‘

โœ…

You can loop through each object of the JSON array and push the close property value to a new array variable.

check this snippet

var arr = [
    {
        "close": 116.59,
        "high": 117.49,
        "low": 116.22,
        "open": 116.57,
        "symbol": "AAPL",
        "volume": 46691331,
        "id": "HISTORICAL_PRICES",
        "key": "AAPL",
        "date": "2020-11-27",
    },
    {
        "close": 114.59,
        "high": 117.49,
        "low": 116.22,
        "open": 116.57,
        "symbol": "AAPL",
        "volume": 46691331,
        "id": "HISTORICAL_PRICES",
        "key": "AAPL",
        "date": "2020-11-27",
    },
    {
        "close": 11.59,
        "high": 117.49,
        "low": 116.22,
        "open": 116.57,
        "symbol": "AAPL",
        "volume": 46691331,
        "id": "HISTORICAL_PRICES",
        "key": "AAPL",
        "date": "2020-11-27",
    }
];

var dataset = [];

arr.forEach((value, key)=>{
    dataset.push(value.close);
});

console.log(dataset);

1๐Ÿ‘

var arr = [
    {
    "close": 116.59,
    "high": 117.49,
    "low": 116.22,
    "open": 116.57,
    "symbol": "AAPL",
    "volume": 46691331,
    "id": "HISTORICAL_PRICES",
    "key": "AAPL",
    "date": "2020-11-27",
},
    {
    "close": 105.3,
    "high": 117.49,
    "low": 116.22,
    "open": 116.57,
    "symbol": "AAPL",
    "volume": 46691331,
    "id": "HISTORICAL_PRICES",
    "key": "AAPL",
    "date": "2020-11-27",
},
];

var data = arr.filter(item => item?.close).map(item => item.close);

console.log(data);

Leave a comment