I have a over 100+ JSON objects with this format:
{
"grants":[
{
"grant":0,
"ID": "EP/E027261/1",
"Title": "Semiconductor Research at the Materials-Device Interface",
"PIID": "6674",
"Scheme": "Platform Grants",
"StartDate": "01/05/2007",
"EndDate": "31/10/2012",
"Value": "800579"
}, ... more grants
I want to be able to grab EndDate and Value into a new array. Like the following output.
"extractedGrants":[
{
"EndDate": "31/10/2012",
"Value": "800579"
}, ... more extracted objects with EndDate and Value properties.
I believe the correct approach is to use array.map(function (a) {}); but I cannot get the code right inside the function.
Thanks.
1 Answer 1
You could use a destruction of the object and a new object for the result array.
var object = { grants: [{ grant: 0, ID: "EP/E027261/1", Title: "Semiconductor Research at the Materials-Device Interface", PIID: "6674", Scheme: "Platform Grants", StartDate: "01/05/2007", EndDate: "31/10/2012", Value: "800579" }] },
result = object.grants.map(({ EndDate, Value }) => ({ EndDate, Value }));
console.log(result);
ES5
var object = { grants: [{ grant: 0, ID: "EP/E027261/1", Title: "Semiconductor Research at the Materials-Device Interface", PIID: "6674", Scheme: "Platform Grants", StartDate: "01/05/2007", EndDate: "31/10/2012", Value: "800579" }] },
result = object.grants.map(function (a) {
return { EndDate: a.EndDate, Value: a.Value };
});
console.log(result);
answered Jul 30, 2017 at 9:56
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js