Reputation: 1834
so i have an array .. with many objects, .. but i want to modify what is inside each object.. but not sure how to go about it. before
[{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: 2018-01-30T00:00:00.000Z,
end: 2018-02-01T00:00:00.000Z,
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
desired after
[
{
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: 2018-01-30T00:00:00.000Z,
end: 2018-02-01T00:00:00.000Z,
address: '101 Great American Pkwy, Santa Clara, CA',
}
]
this array only has one object for this example, but it would be many objects i would be modifying in the same way ..(edited) i think .map should be used.. but thats all i know
Upvotes: 0
Views: 56
Reputation: 63560
If/when you're comfortable with ES6 syntax you can do this quite concisely:
const out = arr.map(obj => {
const { layout, ...rest } = obj.info;
return { ...obj, info: rest }
});
map
over the array of objects, use object destructuring to grab the layout
property, and then ...rest
(spread syntax) for all the other properties. Then just return the object assigning only the properties gathered in rest
to the nested info object.
Upvotes: 0
Reputation: 18401
Here, it is a generic way to have a new array that contains only the properties of the nested object. Using typeof
, you check if the value of the property is an object and Object.assign
to copy the object properties.
var data = [{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: "2018-01-30T00:00:00.000Z",
end: "2018-02-01T00:00:00.000Z",
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
var newArray = [];
data.forEach(element => {
var newObj = {};
Object.keys(element).forEach(key => {
if(typeof element[key] === "object"){
Object.assign(newObj, element[key]);
newArray.push(newObj);
}
})
})
console.log(newArray)
Upvotes: 0
Reputation: 11297
You can use delete
command to remove info.layout
var a = [{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: '2018-01-30T00:00:00.000Z',
end: '2018-02-01T00:00:00.000Z',
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
a = a.map(o => {
var o = o.info;
delete o['layout'];
return o;
});
console.log(a);
Upvotes: 0
Reputation: 32206
const myArray = [{
filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info: {
layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: "2018-01-30T00:00:00.000Z",
end: "2018-02-01T00:00:00.000Z",
address: '101 Great American Pkwy, Santa Clara, CA'
}
}];
// Do it manually:
const mapped = myArray.map(x => ({
permalink: x.info.permalink,
title: x.info.title,
location: x.info.location,
description: x.info.description,
start: x.info.start,
end: x.info.end,
address: x.info.address
}));
// Or define a set of keep keys and discard the rest:
const keepKeys = [
"permalink",
"title",
"location",
"description",
"start",
"end",
"address"
];
const otherMapped = myArray.map(x => Object.keys(x.info).reduce((newObj, key) => {
if (keepKeys.includes(key)) newObj[key] = x.info[key];
return newObj;
}, {}));
console.log(mapped);
console.log(otherMapped);
Upvotes: 0
Reputation: 1480
var myArray = [{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: '2018-01-30T00:00:00.000Z',
end: '2018-02-01T00:00:00.000Z',
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
console.log(myArray.map(function(arrayItem) {
delete arrayItem.info.layout;
return arrayItem.info
}));
Upvotes: 0
Reputation: 327
arr.map(function(obj) {
return {
permalink: obj.info.permalink,
title: obj.info.title,
location: obj.info.title,
description: obj.info.descriptioin,
start: obj.info.start,
end: obj.info.end,
address: obj.info.address,
}
})
Upvotes: 2