1

Below is my Json

"Issues": [
    {
        "Id": null,
        "Key": "Project20",
        "Values": [
            {
                "Key": "Display Name",
                "Value": "Rya"
            },
            {
                "Key": "UserName",
                "Value": "RH"
            },
            {
                "Key": "Count",
                "Value": "350"
            }
        ]
    },
    {
        "Id": null,
        "Key": "Project30",
        "Values": [
            {
                "Key": "Display Name",
                "Value": "Mike"
            },
            {
                "Key": "UserName",
                "Value": "ML"
            },
            {
                "Key": "Count",
                "Value": "90"
            }
        ]
    }
]

I need to map this Json to form the below array

{ "Display Name": 'Rya', "UserName" : "RH", value: 350, url: "Project20" },
{ "Display Name": 'Mike', "UserName" : "ML", value: 90, url:"Project30" }

Basically, I need to get the Key also in my array.

I tried

Issues.map(o => o.Values.reduce((acc, {Key, Value}) => (acc[Key] = Value, acc), {}));

this gives me

{ "Display Name": 'Rya', "UserName" : "RH", value: 350 },
{ "Display Name": 'Mike', "UserName" : "ML", value: 90 }

But I need the Key field also in the array

1 Answer 1

4

Use the initial value argument of reduce. So instead of {} pass { url: o.Key }:

Issues.map(o => o.Values.reduce((acc, {Key, Value}) => (acc[Key] = Value, acc),
                                { url: o.Key }));

For those on IE, you'll need to use the ES5-compatible syntax:

Issues.map(function (o) {
    return o.Values.reduce(function (acc, pair) {
        acc[pair.Key] = pair.Value;
        return acc;
    }, { url: o.Key });
});

var Issues = [
    {
        "Id": null,
        "Key": "Project20",
        "Values": [
            {
                "Key": "Display Name",
                "Value": "Rya"
            },
            {
                "Key": "UserName",
                "Value": "RH"
            },
            {
                "Key": "Count",
                "Value": "350"
            }
        ]
    },
    {
        "Id": null,
        "Key": "Project30",
        "Values": [
            {
                "Key": "Display Name",
                "Value": "Mike"
            },
            {
                "Key": "UserName",
                "Value": "ML"
            },
            {
                "Key": "Count",
                "Value": "90"
            }
        ]
    }
];

var result = Issues.map(function (o) {
    return o.Values.reduce(function (acc, pair) {
        acc[pair.Key] = pair.Value;
        return acc;
    }, { url: o.Key });
});

console.log(result);

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

7 Comments

Does this work in IE? I couldn't able to test it. If it doesn't work, is there a tool to convert it, as I used similarly in many parts of my code
Did you get a chance to see my comment
Since you had provided ES6 syntax in your question which gave you a result, I had assumed you had ES6 support. See update to my answer, which includes an ES5 version.
Thanks trincot. I came to know that recently. Thanks for your help. Is there any tool where I can convert my other ES6 code.
Yes, there is: babeljs.io/repl Make sure in the presets drop-down you select ES2015.
|

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.