简体   繁体   中英

Extract 3 arrays of data from Array of JSON objects

I have a array of JSON objects defined as follow:

[
    {   
        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]]
            }
        }
    },...
]

I would like to retrieve 3 distincts arrays:

  • One containing the name of distinct names that exist in all objects : result = ["Mercedes", "Benz", "Toty"]
  • One containing all distinct values of type: type = ["ClassA", "ClassB"]
  • One containing all distinct values of numbers : numbers = ["1", "2", "3"]

How can i retrieve these 3 arrays without needed to right multiple loops ?

This is not perfect, could be done in cleaner more "JS-y" ways, but here ya go

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

Check out reduce - one of the many possible ways you can do it.

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

This is just a proof of concept, but I think it can be made a recursive function and be more elegant.

 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); 
 .as-console-wrapper {max-height: 100% !important;top: 0;} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM