Javascript:从嵌套键值对中读取键/值

时间:2017-04-09 22:17:16

标签: javascript arrays javascript-objects

我正在尝试从嵌套键/值Javascript对象输出 Category 属性作为数组,如下所示。

var data = [{key: "A", values:[{Category:"One", amount:2000},
    {Category: "Two", amount:2500},
    {Category: "Three", amount: 3000},
    {Category: "Four", amount: 3000}]
},
{key: "B", values:[{Category:"One", amount:2000},
    {Category: "Two", amount:2500},
    {Category: "Three", amount: 3000},
    {Category: "Four", amount: 3000}]
},
{key: "C", values:[{Category:"One", amount:2000},
    {Category: "Two", amount:2500},
    {Category: "Three", amount: 3000},
    {Category: "Four", amount: 3000}]
}]  

预期输出如下: -

["One","Two","Three","Four"]  

我尝试了几种方法来实现这一目标 我最接近的是嵌套地图功能,如下所示 下面的数据是一个包含上面Javascript对象的变量。

x = data.map(function(el) {return el.values.map(function(ele,i){return 
ele.Category;})})  

但是输出仍然是一个数组数组,如下所示。我可以对此进行切片并只读取其中一个返回的数组,但我确信有更好的方法可以做到这一点。

[["One","Two","Three","Four"],["One","Two","Three","Four"],
["One","Two","Three","Four"]]  

任何建议都将不胜感激。

4 个答案:

答案 0 :(得分:4)

您可以首先展平返回的数组数组,然后使用Set删除所有重复的条目。



var data = [{key:"A",values:[{Category:"One",amount:2000},{Category:"Two",amount:2500},{Category:"Three",amount:3000},{Category:"Four",amount:3000}]},{key:"B",values:[{Category:"One",amount:2000},{Category:"Two",amount:2500},{Category:"Three",amount:3000},{Category:"Four",amount:3000}]},{key:"C",values:[{Category:"One",amount:2000},{Category:"Two",amount:2500},{Category:"Three",amount:3000},{Category:"Four",amount:3000}]}],
    x = data.map(v => v.values.map(c => c.Category)).reduce((a,b) => a.concat(b)),
    res = [...new Set(x)];

    console.log(res);




答案 1 :(得分:2)

这是一种不那么神秘的方式。你的设置有点有趣,因为有重复,你似乎想要消除欺骗。 ES6套装非常适合。这可以获得您要求的结果。

let data = [{key:"A",values:[{Category:"One",amount:2000},{Category:"Two",amount:2500},{Category:"Three",amount:3000},{Category:"Four",amount:3000}]},{key:"B",values:[{Category:"One",amount:2000},{Category:"Two",amount:2500},{Category:"Three",amount:3000},{Category:"Four",amount:3000}]},{key:"C",values:[{Category:"One",amount:2000},{Category:"Two",amount:2500},{Category:"Three",amount:3000},{Category:"Four",amount:3000}]}],
    set = new Set();
    data.forEach(val => {
        let v = val.values.forEach ( x => {
            set.add(x.Category);
        });
    });

    console.log(Array.from(set));

答案 2 :(得分:0)

我使用Underscore.js

var data = [
    {
        key: "A",
        values: [
            {Category: "One",   amount: 2000},
            {Category: "Two",   amount: 2500},
            {Category: "Three", amount: 3000},
            {Category: "Four",  amount: 3000}
        ]
    },
    ...
];

var categories =
  _.unique(
    _.flatten(
      _.map(data, function (item) {
        return _.pluck(item.values, 'Category');
      })
    )
  );

答案 3 :(得分:0)



var data = [{key: "A", values:[{Category:"One", amount:2000},
    {Category: "Two", amount:2500},
    {Category: "Three", amount: 3000},
    {Category: "Four", amount: 3000}]
},
{key: "B", values:[{Category:"One", amount:2000},
    {Category: "Two", amount:2500},
    {Category: "Three", amount: 3000},
    {Category: "Four", amount: 3000}]
},
{key: "C", values:[{Category:"One", amount:2000},
    {Category: "Two", amount:2500},
    {Category: "Three", amount: 3000},
    {Category: "Four", amount: 3000}]
}
];


var ans = data.reduce(function(acc, el) {
    return acc.concat(el.values.map(x => x.Category));
}, [])

var result = [...new Set(ans)]
console.log(result);