I have an object that looks like this:
{
\"1\": \"Technology\",
\"2\": \"Startup\",
\"3\": \"IT\",
}
and I need to convert it to an a
Assuming your object instance is named obj
:
Object.keys(obj).reduce((acc, curr) => {
return [...acc, { id: curr, name: obj[curr] }]
}, [])
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]
});
};
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: