As you can see i have a multiple object like “123”, “098”, and “456”, i want all of those object to be inside the object of multiple object.
Example:
JavaScript
x
16
16
1
var obj =
2
{
3
"123": {
4
"name": "yourname1"
5
"class": ["art","math"]
6
},
7
"098": {
8
"name": "yourname2"
9
"class": ["art"]
10
},
11
"456": {
12
"name": "yourname3"
13
"class": ["math"]
14
}
15
}
16
output i expected:
JavaScript
1
17
17
1
{
2
"number": "123",
3
"name": "yourname1"
4
"class": ["art","math"]
5
},
6
{
7
"number": "098"
8
"name": "yourname2"
9
"class": ["art"]
10
},
11
{
12
"number": "456"
13
"name": "yourname3"
14
"class": ["math"]
15
}
16
}
17
How can I achieve this? I’m sorry im not show any javascript code cause until now I have no idea what to do
Advertisement
Answer
JavaScript
1
16
16
1
var obj = {
2
123: {
3
name: 'yourname1',
4
},
5
'098': {
6
name: 'yourname2',
7
},
8
456: {
9
name: 'yourname3',
10
},
11
};
12
const obj2 = Object.keys(obj).map(key => ({
13
number: key,
14
name: obj[key].name,
15
}));
16
console.log(obj2);