0

i have the following object array

 datasets: [{
    data: [],
    backgroundColor: [
        "#FF6384",
        "#4BC0C0",
        "#FFCE56",
        "#E7E9ED",
        "#36A2EB"
    ],
    label: 'My dataset' // for legend
}],
labels: []
};

and i have another object array like bellow

[
{
"PORT": "MY",
"Country Name": "AUSTRALIA",
"nocontainers": "1017"
},
{
"PORT": "MY"
"Country Name": "CAMBODIA",
"nocontainers": "1"
},
{
"PORT": "DE"
"Country Name": "CHINA",
"nocontainers": "13846"
},
{
"PORT": "DE"
"Country Name": "HONG KONG",
"nocontainers": "252"
},
{
"PORT": "MY"
"Country Name": "INDONESIA",
"nocontainers": "208"
}

what i want to is to push all the value from 'nocontainers' to 'data' key and value from 'Country Name' to 'labels' key in first array.

i have tried array.push but didn't work, my final array should look like bellow

 datasets: [{
    data: [1017, 1, 13846, 252, 208],
    backgroundColor: [
        "#FF6384",
        "#4BC0C0",
        "#FFCE56",
        "#E7E9ED",
        "#36A2EB"
    ],
    label: 'My dataset' // for legend
}],
labels: ["AUSTRALIA (MY)","CAMBODIA (MY)","CHINA (DE)","HONG KONG (DE)","INDONESIA (MY)"]
};
0

2 Answers 2

1

You can create your object using .map with destructuring assignment to pull out the required properties from your countries object.

See working example below:

const countries = [{PORT:"MY","Country Name":"AUSTRALIA",nocontainers:"1017"},{PORT:"MY","Country Name":"CAMBODIA",nocontainers:"1"},{PORT:"DE","Country Name":"CHINA",nocontainers:"13846"},{PORT:"DE","Country Name":"HONG KONG",nocontainers:"252"},{PORT:"MY","Country Name":"INDONESIA",nocontainers:"208"}];

const obj = {
  datasets: {
    data: [],
    backgroundColor: ["#FF6384", "#4BC0C0", "#FFCE56", "#E7E9ED", "#36A2EB"],
    label: 'My dataset'
  },
  labels: []
};

obj.datasets.data = countries.map(({nocontainers: nc}) => +nc);
obj.labels = countries.map(({"Country Name": cn, PORT: p}) => `${cn} (${p})`);

console.log(obj);

Sign up to request clarification or add additional context in comments.

6 Comments

Note that he wanted to change original object, maybe you should provide an alternative example: obj.datasets.data = countries.map(...) and obj.labels = countries.map(...). Anyway +1!
what if i want to combined new port value to country object in brackets?
@Eliteuser I don't really understand your question. Pease edit your question (leaving the original untouched) with your updated question, providing the expected output.
@Shidersz thanks, I decided to change my original answer :)
@NickParsons sorry about that. i was just thinking what is i want to 'PORT' value to be displayed in front of in new array like this labels: ["AUSTRALIA (MY)","CAMBODIA (MY)","CHINA (DE)","HONG KONG (DE)","INDONESIA (MY)"]
|
0

let data = {
  datasets: {
    data: [],
    backgroundColor: [
      "#FF6384",
      "#4BC0C0",
      "#FFCE56",
      "#E7E9ED",
      "#36A2EB"
    ],
    label: 'My dataset' // for legend
  },
  labels: []
}
let arr = [{
    "Country Name": "AUSTRALIA",
    "nocontainers": "1017"
  },
  {
    "Country Name": "CAMBODIA",
    "nocontainers": "1"
  },
  {
    "Country Name": "CHINA",
    "nocontainers": "13846"
  },
  {
    "Country Name": "HONG KONG",
    "nocontainers": "252"
  },
  {
    "Country Name": "INDONESIA",
    "nocontainers": "208"
  }
]
arr.forEach(a => {
  data.datasets.data.push(a['nocontainers'])
  data.labels.push(a['Country Name'])
})
console.log(data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.