Reputation: 1089
Consider this data:
{
"id": 1,
"title": "Title one",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
I loop through my data to all the categories using this function:
remove_category_duplicates: function () {
// Get all categories and remove duplicates
let data = {}
this.info.forEach(i=>{
Object.assign(data,i.category_data);
})
return data;
},
This returns an object, like so:
Object {
12: "Fax",
2: "Team",
6: "Questions",
7: "Queries"
}
How can I also return just the value (Ex, Fax)? I want to then push those name values into an array.
Thanks
Upvotes: 1
Views: 66
Reputation: 121
You can push values by this way
var info = [{
"id": 1,
"title": "Title one",
"category_data": {
"2": "Team",
"7": "Queries"
}
}];
var remove_category_duplicates = function () {
// Get all categories and remove duplicates
let data = [];
for (var i in info) {
if (info[i] && info[i].category_data) {
var category_data=info[i].category_data;
for (var j in category_data) {
data.push(category_data[j]);
}
}
}
console.log(data)
return data;
};
remove_category_duplicates();
Upvotes: 1
Reputation: 349
To return only values you can use the Array.map
function like this:
let arr = this.info.map(val => val.category_data);
...or use the newer Object.values()
as mentioned in the comments ;)
Upvotes: 3