I found some solutions but no exactly what I want. I want to change field name inside object name where condition I will explain by following example for if array=
[{
"mobile1": [{
"screensize": "6.5"
}]
},
{
"mobile2": [{
"screensize": "6.5"
},
{
"price": "2000"
}
]
}
]
I want to change mobile1 to newmobile final output will be
[{
"newmobile": [{
"screensize": "6.5"
}]
},
{
"mobile2": [{
"screensize": "6.5"
},
{
"price": "2000"
}
]
}
]
CodePudding user response:
With input.map(i => { const {x, ...rest} = i; return x ? {...rest, a: x} : rest})
you should be able to change const input = [{x: 1, y: 2}, {x: 11, y: 22}, {z: 111, y: 222}]
into [{y:2,a:1},{y:22,a:11},{z:111,y:222}]
.
Could this work for you?
CodePudding user response:
Try this:
let c = [{
"mobile1": [{
"screensize": "6.5"
}]
},
{
"mobile2": [{
"screensize": "6.5"
},
{
"price": "2000"
}
]
}
]
let final = c.map(i=>{
let x = Object.getOwnPropertyNames(i)
if(x=="mobile1"){
console.log(i.mobile1)
return {"newmobile":i.mobile1}
}
else {
return i
}
})
console.log("final output",final);