Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

How to pass array values to another array inside for loop Javascript

Hello i have this code that makes a graphic using chartJs, the code gets the labels from Json and values from another function, this is the code that fills the graph:

async function outlayGraph(label) {

    const context = document.getElementById('outlayGraph').getContext('2d');

    let data = 0;
    let configuration = 0;
    let allCurrencyName = [];

    for (i in label) {
        const item = label[i];
        allCurrencyName.push(item.name);
    }

    data = {
        labels: created_date,
        datasets: allCurrencyName.map(currency => ({
            label: currency,
            borderColor: generateRandomColor(),
            data: albanianLek
        }))
    };

    configuration = {

        type: 'line',

        data,

        options: {
            tooltips: {
                mode: 'index'
            }
        }
    };
  console.log(albanianLek);
    if (charts) {
        charts.data.labels = created_date;
        charts.data.datasets[i].data = albanianLek;
        charts.update();
    } else {
        charts = new Chart(context, configuration);
    }
}

The data inside dataset gets the numeric values and these values i get them from this function:

function fillOutlayGraph(barChatData) {

    const allCurrency = [];

    for (i in barChatData) {

        const item = barChatData[i];
        allCurrency.push(item.currencies);
    }

    albanianLek = allCurrency.map(values => ({
        values
    }))
}

The values come like this:

0: Object { values: {…} }
​​
values: Object { ALL: 289 }
​​
<prototype>: Object { … }
​
1: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
2: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
3: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
4: Object { values: {…} }
​​
values: Object { USD: 100 }
​​
<prototype>: Object { … }
​
5: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
6: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
7: Object { values: {…} }
​​
values: Object { ALL: 300, USD: 250 }
​​
<prototype>: Object { … }
​
8: Object { values: {…} }
​​
values: Object { ALL: 130, USD: 200 }
​​
<prototype>: Object { … }
​
9: Object { values: {…} }
​​
values: Object { USD: 200 }
​​
<prototype>: Object { … }
​
10: Object { values: {…} }
​​
values: Object { ALL: 1000, EUR: 2000, GBP: 500 }
​​
<prototype>: Object { … }
​
11: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
12: Object { values: {…} }
​​
values: Object { ALL: 0, USD: 0, EUR: 0, … }
​​
<prototype>: Object { … }
​
13: Object { values: {…} }
​​
values: Object { ALL: 500, EUR: 900 }

I am using one array to have all the values and pass them with for loop

0 likes
1 reply
LaryAI's avatar
Level 58

You can use the map() method to iterate through the allCurrency array and extract the values from each object. Then you can use the Object.values() method to get an array of the values from each object. Finally, you can use the spread operator (...) to spread the values into a new array.

function fillOutlayGraph(barChatData) {

    const allCurrency = [];

    for (i in barChatData) {

        const item = barChatData[i];
        allCurrency.push(item.currencies);
    }

    albanianLek = allCurrency.map(values => Object.values(values)).flat();
}

Please or to participate in this conversation.