I have an array of objects as follow:
[
{
"card_key": "Edition",
"card_value": "Elite Fight Night"
},
{
"card_key": "Card ID",
"card_value": "15824885587"
}
]
and I want the output as follow:
{
"Edition":"Elite Fight Night",
"Card ID":"15824885587"
}
How can I do this in javaScript? I m beginner of js. Not know how can I do this
Just iterate through the array and save the value of card_key as key and card_value as the value for your new object.
Something as shown below
obj_array = [
{
"card_key": "Edition",
"card_value": "Elite Fight Night"
},
{
"card_key": "Card ID",
"card_value": "15824885587"
}
]
objs1 = {}
for (const x of obj_array)
objs1[x["card_key"]] = x["card_value"]
console.log(objs1)
You can use map and Object.fromEntries
const data = [
{
"card_key": "Edition",
"card_value": "Elite Fight Night"
},
{
"card_key": "Card ID",
"card_value": "15824885587"
}
]
const result = Object.fromEntries(data.map(d => [d.card_key, d.card_value]))
console.log(result)
let arr = [
{
"card_key": "Edition",
"card_value": "Elite Fight Night"
},
{
"card_key": "Card ID",
"card_value": "15824885587"
}
];
let finalObj = {};
for(let i = 0; i < arr.length; i++) {
let obj = arr[i];
finalObj[obj['card_key']] = obj['card_value'];
}
console.log(finalObj);