Convert Object To An Array Of Objects?
I have an object that looks like this: { '1': 'Technology', '2': 'Startup', '3': 'IT', } and I need to convert it to an array of objects that would look like this: [ {id:
Solution 1:
You can use .map()
with Object.keys()
:
let data = {
"1": "Technology",
"2": "Startup",
"3": "IT",
};
let result = Object.keys(data)
.map(key => ({id: Number(key), name: data[key]}));
console.log(result);
Useful Resources:
Solution 2:
Assuming your object instance is named obj
:
Object.keys(obj).reduce((acc, curr) => {
return [...acc, { id: curr, name: obj[curr] }]
}, [])
Solution 3:
the trivial way
var o = {
"1": "Technology",
"2": "Startup",
"3": "IT",
};
var arr = [];
for(var i in o) {
arr.push({
id: i,
number: o[i]
});
};
Solution 4:
const words = {
told: 64,
mistake: 11,
thought: 16,
bad: 17
}
const results = []
Object.entries(words).map(val => results.push({
text: val[0],
value: val[1]
}))
console.log(results)
Post a Comment for "Convert Object To An Array Of Objects?"