2017-07-28 50 views
0

我是新來的JavaScript,我有JSON數組,其中包含一些重複鍵象下面這樣:從陣列JSON對象查找最大值,並把它們合併

var connections = [ 

    { 
     "source":"l1", 
     "target":"l2", 
     "metrics": { "normal":20 }, 
     "metadata": { "streaming": 1 } 
    }, 
    { 
     "source":"l2", 
     "target":"l3", 
     "metrics": { "normal":30 }, 
     "metadata": { "streaming": 1 } 
    }, 

    { 
     "source":"l2", 
     "target":"l3", 
     "metrics": { "normal":25 }, 
     "metadata": { "streaming": 1 } 
    }, 
    { 
     "source":"l3", 
     "target":"l4", 
     "metrics": { "normal":24 }, 
     "metadata": { "streaming": 1 } 
    }, 
    { 
     "source":"l3", 
     "target":"l4", 
     "metrics": { "normal":21 }, 
     "metadata": { "streaming": 1 } 
    }, 
    { 
     "source":"l3", 
     "target":"l4", 
     "metrics": { "normal":20 }, 
     "metadata": { "streaming": 1 } 
    }, 


] 

現在我想合併重複的JSON誰擁有相同的價值「源」和「目標」和「正常」鍵應該是所有同一來源和目標的最大值。 因此,對於給定的例子答案是:

var answer =[ 
{ 
      "source":"l1", 
      "target":"l2", 
      "metrics": { "normal":20 }, 
      "metadata": { "streaming": 1 } 
     }, 
     { 
      "source":"l2", 
      "target":"l3", 
      "metrics": { "normal":30 }, 
      "metadata": { "streaming": 1 } 
     }, 
     { 
      "source":"l3", 
      "target":"l4", 
      "metrics": { "normal":24 }, 
      "metadata": { "streaming": 1 } 
     }, 

] 

這僅僅是一個例子反對它會與不同的對象。我不知道如何解決這個問題, 我認爲下劃線或lodash可以很容易地解決這個問題,但任何解決方案都可以接受。

回答

1

使用_.groupBy()通過源和目標收集對象。使用_.map()和Array#reduce可以得到每個組中最大值爲normal的值。

var connections = [{"source":"l1","target":"l2","metrics":{"normal":20},"metadata":{"streaming":1}},{"source":"l2","target":"l3","metrics":{"normal":30},"metadata":{"streaming":1}},{"source":"l2","target":"l3","metrics":{"normal":25},"metadata":{"streaming":1}},{"source":"l3","target":"l4","metrics":{"normal":24},"metadata":{"streaming":1}},{"source":"l3","target":"l4","metrics":{"normal":21},"metadata":{"streaming":1}},{"source":"l3","target":"l4","metrics":{"normal":20},"metadata":{"streaming":1}}]; 
 

 
var result = _(connections) 
 
    .groupBy(function(o) { // group by source and target 
 
    return o.source + '-' + o.target; 
 
    }) 
 
    .map(function(arr) { // map the groups to values 
 
    return arr.reduce(function(max, o) { // get the object with the hight normal in each group 
 
     return max.metrics.normal > o.metrics.normal ? max : o; 
 
    }); 
 
    }) 
 
    .value(); 
 

 
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

0

你可以基於在目標和源是相同的,然後檢查是否度量大於所述先前存儲的值

var connections = [ 
 
    {"source": "l1", "target": "l2", "metrics": {"normal": 20}, "metadata": {"streaming": 1} }, 
 
    {"source": "l2", "target": "l3", "metrics": {"normal": 30}, "metadata": {"streaming": 1} }, 
 
    {"source": "l2", "target": "l3", "metrics": {"normal": 25}, "metadata": {"streaming": 1} }, 
 
    {"source": "l3", "target": "l4", "metrics": {"normal": 24}, "metadata": {"streaming": 1} }, 
 
    {"source": "l3", "target": "l4", "metrics": {"normal": 21}, "metadata": {"streaming": 1} }, 
 
    {"source": "l3", "target": "l4", "metrics": {"normal": 20}, "metadata": {"streaming": 1} } 
 
] 
 

 
var res = connections.reduce((a,b) => { 
 
    let i = a.findIndex(item => item.target === b.target && item.source === b.source); 
 
    if (i===-1) { 
 
     a.push(b); 
 
    } else if (a[i].metrics.normal < b.metrics.normal) { 
 
     a[i] = b; 
 
    } 
 
    return a; 
 
}, []); 
 

 
document.body.innerHTML = '<pre>' + JSON.stringify(res,0,4) + '</pre>'
更在陣列降低到一個新的數組

1

您可以使用一個對象作爲地圖, source + target作爲鍵和的connections作爲值的對象,保持具有最高度量的那些用於source + target每種組合:

var connections = [ 
 
    {"source": "l1", "target": "l2", "metrics": {"normal": 20}, "metadata": {"streaming": 1} }, 
 
    {"source": "l2", "target": "l3", "metrics": {"normal": 30}, "metadata": {"streaming": 1} }, 
 
    {"source": "l2", "target": "l3", "metrics": {"normal": 25}, "metadata": {"streaming": 1} }, 
 
    {"source": "l3", "target": "l4", "metrics": {"normal": 24}, "metadata": {"streaming": 1} }, 
 
    {"source": "l3", "target": "l4", "metrics": {"normal": 21}, "metadata": {"streaming": 1} }, 
 
    {"source": "l3", "target": "l4", "metrics": {"normal": 20}, "metadata": {"streaming": 1} } 
 
]; 
 

 
var map = {}; 
 
connections.forEach((connection) => { 
 
    var mapKey = connection.source + "/" + connection.target; 
 
    answerConnection = map[mapKey]; 
 

 
    if (!answerConnection || answerConnection.metrics.normal < connection.metrics.normal) 
 
    map[mapKey] = connection; 
 
}); 
 

 
var answer = Object.values(map); 
 

 
console.log(answer);

相關問題