映射Json来形成一个数组 [英] mapping Json to form an array

查看:104
本文介绍了映射Json来形成一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的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"
            }
        ]
    }
]

我需要映射这个Json以形成以下数组

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.

我试过

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

这给了我

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

但是我还需要数组中的Key字段

But I need the Key field also in the array

推荐答案

使用 reduce 的初始值参数。因此,而不是 {} pass {url:o.Key}

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

对于IE上的用户,您需要使用符合ES5的语法:

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

这篇关于映射Json来形成一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆