将Json对象转换为数组 [英] Converting Json object in to array

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

问题描述

从下面的Json,我正在尝试构建一个如下所示的数组。

 问题:[
{
Id:null,
:null,
值:[
{
键:显示名称,
值:Rya
},
{
Key:UserName,
Value:RH
},
{
Key:Count,
值:350
}
]
},
{
Id:null,
Key ,
值:[
{
键:显示名称,
值:迈克
},
{
Key:UserName,
Value:ML
},
{
Key:Count ,
价值:90
}
]
}
]

所需数组:

  {显示名称:'Rya' UserName:RH,value:350},
{显示名称:Mike,UserName:ML,值:90}
/ pre>

这里,数组中可以有任意数量的字段,但不限于显示名称UserName和值。所以我想动态构建数组。

解决方案

我假设问题是您拥有的对象的财产。



您可以使用此ES6代码,使用 reduce

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

  const obj = {Issues:[{Id:null,Key:null,Values:[{Key:Display Name,Value:Rya}, {Key:UserName,Value:RH},{Key:Count,Value:350}]},{Id:null,Key ,值:[{键:显示名称,值:麦克},{键:用户名,值:ML}, {Key:Count,Value:90}}}}; const result = obj.Issues.map(o => o.Values.reduce((acc,{Key,Value})=>(acc [Key] = Value,acc),{})); console.log(result);  pre> 

  .as-console-wrapper {max-height:100%!顶部:0;或者在ES5兼容的代码中,没有(有点模糊的) )逗号运算符:

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

  var obj = {Issues:[{Id:null,Key:null,Values:[{Key:Display Name,Value:Rya}, {Key:UserName,Value:RH},{Key:Count,Value:350}]},{Id:null,Key ,值:[{键:显示名称,值:迈克},{键:用户名,值:ML}, key:Count,Value:90}]}}} var result = obj.Issues.map(function(o){return o.Values.reduce(function(acc,kv) [kv.Key] = kv.Value;返回},{})}); console.log(result);  

  .as-console-wrapper {max-height:100%!important;顶部:0; }  


From the below Json, I am trying to build an array as shown below.

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

Desired Array:

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

Here, there can be any number of fields in the array, but not confined to "Display Name" "UserName" and "Value". so I want to build that array dynamically.

解决方案

I assume "Issues" is a property of an object you have.

You could use this ES6 code, using reduce:

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

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

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

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

Or in ES5 compatible code, and without the (bit obscure) comma operator:

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

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

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

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于将Json对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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