从JSON对象数组中提取3个数据数组

时间:2018-01-25 21:27:24

标签: javascript jquery

我有一个JSON对象数组,定义如下:

[
    {   
        key : "test1",
        data : {
            "Mercedes" : {
                "ClassA" : [1, 2],
                "ClassB" : [1]
            },
            "Benz" : {
                "ClassA" : [1]]
            }
        }
    },
    {   
        key : "test2",
        data : {
            "Mercedes" : {
                "ClassA" : [1, 2, 3],
                "ClassB" : [1]
            },
            "Toty" : {
                "ClassA" : [1]]
            }
        }
    },...
]

我想检索3个不同的数组:

  • 包含所有对象中存在的不同名称的名称:result = ["Mercedes", "Benz", "Toty"]
  • 一个包含所有类型的不同值:type = ["ClassA", "ClassB"]
  • 包含所有不同数字值的一个:numbers = ["1", "2", "3"]

如何在不需要多个循环的情况下检索这3个数组?

3 个答案:

答案 0 :(得分:1)

这不是完美的,可以用更清洁的“JS-y”方式完成,但是这里你好了

var someArray = ...; // your input array
var uniqueCars = new Set();
var uniqueClasses = new Set();
for (var i = 0; i < someArray.length; i++) {
   // iterate through all prop names
   for (var carProp in someArray[i].data) {
       uniqueCars.add(carProp);

       for (var classProp in someArray[i].data[carProp]) {
           uniqueClasses.add(classProp);

           // Too lazy to do the last one, hopefully you can figure it out
       }
   }    
}

var finalCars = Array.from(uniqueCars);
var finalClasses = Array.from(uniqueClasses);
// do the 3rd one you asked for

答案 1 :(得分:1)

查看reduce - 您可以采用的众多方法之一。

var data = [{
    key: "test1",
    data: {
      "Mercedes": {
        "ClassA": [1, 2],
        "ClassB": [1]
      },
      "Benz": {
        "ClassA": [1]
      }
    }
  },
  {
    key: "test2",
    data: {
      "Mercedes": {
        "ClassA": [1, 2, 3],
        "ClassB": [1]
      },
      "Toty": {
        "ClassA": [1]
      }
    }
  }
];

var thing = data.reduce((acc, itm) => {
  for (var type in itm.data) {
    if (acc.types.indexOf(type) === -1) acc.types.push(type);
    for (var cls in itm.data[type]) {
      if (acc.classes.indexOf(cls) === -1) acc.classes.push(cls);
      for (var i = itm.data[type][cls].length; i--;)
        if (acc.numbers.indexOf(itm.data[type][cls][i]) === -1)
          acc.numbers.push(itm.data[type][cls][i]);
    }
  }
  return acc;
}, {
  types: [],
  numbers: [],
  classes: []
});


console.log('Unique Types', thing.types);
console.log('Unique Numbers', thing.numbers);
console.log('Unique Classes', thing.classes);

答案 2 :(得分:0)

这只是一个概念证明,但我认为它可以成为递归函数并且更加优雅。

&#13;
&#13;
let arr = [{ key: "test1", data: { "Mercedes": { "ClassA": [1, 2], "ClassB": [1] }, "Benz": { "ClassA": [1] } } }, { key: "test2", data: { "Mercedes": { "ClassA": [1, 2, 3], "ClassB": [1] }, "Toty": { "ClassA": [1] } } }],

  flatten = (a, b) => [...a, ...b],
  allUnq = a => [...new Set(a.reduce(flatten))],

  data = arr.map(o => o.data),
  vals = d => d.map(Object.values),
  keys = d => d.map(Object.keys),
  
  arr1 = allUnq(keys(data)),
  arr2 = allUnq(vals(data).map(keys).map(allUnq)),
  arr3 = allUnq(allUnq(vals(allUnq(vals(data)))));

console.log(arr1);
console.log(arr2);
console.log(arr3);
&#13;
.as-console-wrapper {max-height: 100% !important;top: 0;}
&#13;
&#13;
&#13;