I have a response from an api call which is an array of Objects. I want to modify every object by removing the nested object 'company' and nested it directly to the main object. Is there a way to do that with a fast way ?
{
"ip": "127.0.0.1",
"hostname": "test.compute.amazonaws.com",
"city": "São Paulo",
"region": "São Paulo",
"country": "Brazil",
"loc": "-33.5375,-36.31",
"org": "AS16509 Amazon.com, Inc.",
"postal": "01000-000",
"timezone": "America/Sao_Paulo",
"company": {
"name": "Amazon Data Services Brazil",
"domain": "amazon.com",
"type": "hosting"
},
"countryCode": "BR"
}
I want to change the json to this
{
"ip": "127.0.0.1",
"hostname": "test.compute.amazonaws.com",
"city": "São Paulo",
"region": "São Paulo",
"country": "Brazil",
"loc": "-33.5375,-36.31",
"org": "AS16509 Amazon.com, Inc.",
"postal": "01000-000",
"timezone": "America/Sao_Paulo",
"name": "Amazon Data Services Brazil",
"domain": "amazon.com",
"type": "hosting"
"countryCode": "BR"
}
CodePudding user response:
I have used this function but I am checking if there's a faster method.
const formatObj = (obj) =>{
obj.name = obj.company.name;
obj.domain = obj.company.domain;
obj.type = obj.company.type;
delete obj.company
return obj
}
CodePudding user response:
var newList = listFromAPI.map((object) => {
delete object.company
return {...object};
});
here newList will have all the list of objects without company property