How can i get a part of the object and put it in a new element in that object
I have an original Array like this
[
{
stt:1,
code: 24,
name: 'n',
age: 27,
address: 'somewhere',
},
{
stt:2,
code: 82,
name: 'jo',
age: 23,
address: 'tree',
},
{
stt:3,
code: 2,
name: 'con',
age: 23,
address: 'somewhere',
},
]
array that i expected to be a result :
[
{
stt:1,
code: 24,
name: 'n',
employee: {
age: 27,
address: 'somewhere',
}
},
{
stt:2,
code: 82,
name: 'jo',
employee: {
age: 23,
address: 'tree',
}
},
{
stt:3,
code: 2,
name: 'con',
employee: {
age: 23,
address: 'somewhere',
}
},
]
i tried to get first 3 element of object but dont know how to do next
CodePudding user response:
you can use Array.map
for that
like this
const transform = data =>data.map(
({age, address, ...rest}) => ({...rest, employee: {age, address}})
)
const data = [
{
stt:1,
code: 24,
name: 'n',
age: 27,
address: 'somewhere',
},
{
stt:2,
code: 82,
name: 'jo',
age: 23,
address: 'tree',
},
{
stt:3,
code: 2,
name: 'con',
age: 23,
address: 'somewhere',
},
]
console.log(transform(data))