2017-07-07 171 views
1

我有一個數組:JS:組陣列由值嵌套數組

[ 
    { 
    theme: "low battery", 
    Number: "S-10", 
    components: [ "hardware", "battery" ] 
    }, 
    { 
    theme: "bad sync", 
    Number: "S-11", 
    components: [ "software" ] 
    }, 
    { 
    theme: "misc troubles", 
    Number: "S-12", 
    components: [ "hardware", "software" ] 
    } 
] 

所以想要組這個數組由「組件」的值。換句話說,我想知道,組件在數組中發生了多少次。例如:

[ 
    { 
    component: "hardware", 
    amount: 2 
    }, 
    { 
    component: "software", 
    amount: 2 
    }, 
    {  
    component: "battery", 
    amount: 1 
    } 
] 
+1

的可能的複製[什麼是GROUPBY對象的JavaScript的陣列上的最有效的方法?(https://stackoverflow.com/questions/14446511/what-is-the最有效的方法到對象上的一個javascript對象數組) – Andreas

回答

1

可以創建地圖來存儲每個組件的計以有效的方式(查找O(1)的複雜性):

var items = [ 
 
    { 
 
    theme: "low battery", 
 
    Number: "S-10", 
 
    components: [ "hardware", "battery" ] 
 
    }, 
 
    { 
 
    theme: "bad sync", 
 
    Number: "S-11", 
 
    components: [ "software" ] 
 
    }, 
 
    { 
 
    theme: "misc troubles", 
 
    Number: "S-12", 
 
    components: [ "hardware", "software" ] 
 
    } 
 
]; 
 

 
var componentMap = new Map(); 
 

 
items.forEach((item) => { 
 
    item.components.forEach((component) => { 
 
    if (componentMap.has(component)) { 
 
     componentMap.set(component, componentMap.get(component) + 1); 
 
    } 
 
    else { 
 
     componentMap.set(component, 1); 
 
    } 
 
    }); 
 
}); 
 

 
var componentCount = []; 
 

 
componentMap.forEach((value, key) => { 
 
    componentCount.push({ 
 
    component: key, 
 
    amount: value 
 
    }); 
 
}); 
 

 
console.log(componentCount);

1


 

 
var arr = [ 
 
     { 
 
     theme: "low battery", 
 
     Number: "S-10", 
 
     components: [ "hardware", "battery" ] 
 
     }, 
 
     { 
 
     theme: "bad sync", 
 
     Number: "S-11", 
 
     components: [ "software" ] 
 
     }, 
 
     { 
 
     theme: "misc troubles", 
 
     Number: "S-12", 
 
     components: [ "hardware", "software" ] 
 
     } 
 
    ]; 
 
    var components = {}; 
 
    arr.forEach(function(val){ 
 
     val.components.forEach(function(c){ 
 
      components[c] = !(c in components) ? 1 : Number(components[c])+1; 
 
     }); 
 
    }); 
 

 
    var componentCount = []; 
 
    for(key in components){ 
 
    componentCount.push({'component' : key, 'amount' : components[key]}); 
 
    } 
 
    
 
    console.log(componentCount);

+0

這正是我需要的。非常感謝。 –

+0

@DenisovJr。考慮接受答案,如果有幫助。乾杯! – Dij

1

您可以使用forEach()循環和thisArg參數來存儲每個組件和增量。

var data = [{"theme":"low battery","Number":"S-10","components":["hardware","battery"]},{"theme":"bad sync","Number":"S-11","components":["software"]},{"theme":"misc troubles","Number":"S-12","components":["hardware","software"]}] 
 

 
var result = [] 
 

 
data.forEach(function(e) { 
 
    var that = this; 
 
    e.components.forEach(function(c) { 
 
    if(!that[c]) { 
 
     that[c] = {component: c, amount: 0} 
 
     result.push(that[c]) 
 
    } 
 
    that[c].amount += 1 
 
    }) 
 
}, {}) 
 

 
console.log(result)