I'm trying to rework some of my API data locally on my systems, but I'm having trouble in JavaScript, I have a loop running for each name on a table, I wanna basically go from this
[
{
"Name": "USERNAME",
"Status": 1234,
"information": "Has something here"
}
...
]
To something like this
[
"USERNAME": {
"Status": 1234,
"information: "Has something here"
}
...
]
How I'm currently running my code is as follows
let array = []
rows.forEach((row) => {
let obj = {
Name: row.name,
Status: 1234,
"information": "Has Something here"
}
array.push(obj)
}
I've already tried a few things including building the array first with the names and adding the objects under each name which seems I'm unable to in the end.
I've also tried pushing to the array as follows array.push({[row.name]: {"Status": 1234, "information": "Has something here"}
but the outcome is not desirable
[
{
"USERNAME": {
"Status": 1234,
"information": "Has something here"
}
}
...
]
But still I don't output as follows
[
"USERNAME": {
"Status": 1234,
"information": "Has something here"
}
...
]
Your question isn't completely clear, but I think you might be looking for one of these approaches:
const arr = [
{
"Name": "USERNAME",
"Status": 1234,
"information": "Has something here"
},
{
"Name": "USERNAME2",
"Status": 5678,
"information": "Has something here"
},
]
console.log(Object.fromEntries(arr.map(({Name,...o})=>[Name, o])))
console.log(arr.map(({Name,...o})=>({[Name]: o})))