1

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

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.