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)"]
};
2 Answers 2
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);
answered Mar 11, 2019 at 3:39
Nick Parsons
51.7k6 gold badges62 silver badges80 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Shidersz
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!Elite user
what if i want to combined new port value to country object in brackets?
Nick Parsons
@Eliteuser I don't really understand your question. Pease edit your question (leaving the original untouched) with your updated question, providing the expected output.
Nick Parsons
@Shidersz thanks, I decided to change my original answer :)
Elite user
@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)"]
|
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)
answered Mar 11, 2019 at 3:39
holydragon
6,7787 gold badges49 silver badges77 bronze badges
Comments
lang-js