2016-09-23 121 views
0

我有形式表格嵌套JSON對象數組從給定的JSON陣列

[{'from':'a','to':'b','type':'add','value':'100','op':'cr'}, 
{'from':'a','to':'b','type':'add','value':'200','op':'dr'}, 
{'from':'a','to':'b','type':'add','value':'300','op':'cr'}, 
{'from':'c','to':'d','type':'sub','value':'400','op':'dr'}, 
{'from':'c','to':'d','type':'sub','value':'500','op':'cr'}] 

的JSON數組我想要的輸出

[{'from':'a','to':'b','add':[{'100':'cr'},{'200':'dr'},{'300':'cr'}]}, 
{'from':'c','to':'d','sub':[{'400':'dr'},{'500':'cr'}]}] 

如何做到這一點的Javascript /的NodeJS?

+1

有沒有什麼神奇的方法來做到這一點,你將需要編寫自己的算法。請展示使用您可能已經嘗試的內容以及您遇到的任何問題。 –

+0

你最好使用JavaScript對象,JSON字符串將是棘手的工作......'如何做到這一點在JavaScript?' - 通過編寫一些代碼 - 這不是icanhazcode.com –

回答

0

您可以使用一個對象作爲散列表,並通過密鑰分配值,其中fromto的部分。

var data = [{ from: 'a', to: 'b', option: '100' }, { from: 'a', to: 'b', option: '200' }, { from: 'a', to: 'b', option: '300' }, { from: 'c', to: 'd', option: '400' }, { from: 'c', to: 'd', option: '500' }], 
 
    grouped = []; 
 

 
data.forEach(function (a) { 
 
    var key = [a.from, a.to].join('|'); 
 
    if (!this[key]) { 
 
     this[key] = { from: a.from, to: a.to, option: [] }; 
 
     grouped.push(this[key]); 
 
    } 
 
    this[key].option.push(a.option); 
 
}, Object.create(null)); 
 

 
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

JS小提琴:https://jsfiddle.net/6wqkhms3/1/

var data = [{'from':'a','to':'b','option':'100'}, 
 
    {'from':'a','to':'b','option':'200'}, 
 
    {'from':'a','to':'b','option':'300'}, 
 
    {'from':'c','to':'d','option':'400'}, 
 
    {'from':'c','to':'d','option':'500'}]; 
 
    
 
    var out = []; 
 
    
 
    // Utility function which finds-out if this object is available in the RESULT array or not 
 
    function findObj(list, item) { 
 
     var resultObj; 
 
     for (var i in list) { 
 
     if (list[i].from === item.from && list[i].to === item.to) { 
 
      resultObj = list[i]; 
 
      break; 
 
     } 
 
     } 
 
    
 
     return resultObj; 
 
    } 
 
    
 
    // EXECUTION 
 
    for (var i in data) { 
 

 
     // Check if this objec is available in the RESULT array, 
 
     if (findObj(out, data[i])) { 
 

 
     // If yes, then push the value to it 
 
     findObj(out, data[i]).option.push(data[i].option); 
 
     } else { 
 
     // If NO, then add this item to the RESULT array 
 
     out.push({ 
 
      from: data[i].from, 
 
      to: data[i].to, 
 
      option: [data[i].option] 
 
     }); 
 
     } 
 
    } 
 
    
 
    console.log(out);

0

試試下面的代碼片段 -

'use strict'; 
var x = [{ 'from': 'a', 'to': 'b', 'option': '100' }, 
    { 'from': 'a', 'to': 'b', 'option': '200' }, 
    { 'from': 'a', 'to': 'b', 'option': '300' }, 
    { 'from': 'c', 'to': 'd', 'option': '400' }, 
    { 'from': 'c', 'to': 'd', 'option': '500' } 
]; 

var match = false; 

x.reduce(function(returnVal, item) { 
    match = false; 
    returnVal.map(function(each) { 
    if (each.from === item.from && each.to === item.to) { 
     if (Array.isArray(each.option)) { 
     each.option.push(item.option); 
     } else { 
     each.option = [each.option]; 
     each.option.push(item.option); 
     } 
     match = true; 
    } 
    return each; 
    }) 
    if (!match) { 
    returnVal.push(item); 
    } 
    return returnVal; 
}, []); 
0

var array1 = [ 
 
    {'from':'a','to':'b','option':'100'}, 
 
    {'from':'a','to':'b','option':'200'}, 
 
    {'from':'a','to':'b','option':'300'}, 
 
    {'from':'c','to':'d','option':'400'}, 
 
    {'from':'c','to':'d','option':'500'} 
 
]; 
 

 
var array2 = []; 
 

 
for(var i=0; i<array1.length; i++) { 
 
    var obj = null, 
 
     from = array1[i]['from'], 
 
     to = array1[i]['to']; 
 

 
    for(var j=0; j<array2.length; j++) { 
 
     if (array2[j]['from'] == from && array2[j]['to'] == to) { 
 
      obj = array2[j]; 
 
      break; 
 
     } 
 
    } 
 

 
    if (obj == null) { 
 
     obj = {'from':from,'to':to,'option':[]}; 
 
     array2.push(obj); 
 
    } 
 

 
    obj['option'].push(array1[i]['option']); 
 
} 
 

 
console.log(array2);

+1

我建議你打破循環在obj = array2 [j]之後; – Laurianti

+1

您有權利+1 @Laurianti – pbaris

0

使用一個簡單的循環通過鍵,其中從 '從' 組合的 'TMP' 對象來迭代和 '到' 可能有幫助:

  var input = [{'from':'a','to':'b','option':'100'}, 
         {'from':'a','to':'b','option':'200'}, 
         {'from':'a','to':'b','option':'300'}, 
         {'from':'c','to':'d','option':'400'}, 
         { 'from': 'c', 'to': 'd', 'option': '500' }]; 
      var tmp = {}; 
      $.each(input, function (idx, obj) { 
       var key = obj.from + obj.to 
       tmp[key] = tmp[key] || { from: obj.from, to: obj.to}; 
       tmp[key].option = tmp[key].option || []; 
       tmp[key].option.push(obj.option); 
      }); 
      var output = []; 
      for(var key in tmp) 
      { 
       output.push(tmp[key]); 
      } 
0

Wihtout jQuery的,只javascript和交叉browswer:

var array = [ 
 
\t { 
 
\t \t 'from': 'a', 
 
\t \t 'to': 'b', 
 
\t \t 'option': '100' 
 
\t }, 
 
\t { 
 
\t \t 'from': 'a', 
 
\t \t 'to': 'b', 
 
\t \t 'option': '200' 
 
\t }, 
 
\t { 
 
\t \t 'from': 'a', 
 
\t \t 'to': 'b', 
 
\t \t 'option': '300' 
 
\t }, 
 
\t { 
 
\t \t 'from': 'c', 
 
\t \t 'to': 'd', 
 
\t \t 'option': '400' 
 
\t }, 
 
\t { 
 
\t \t 'from': 'c', 
 
\t \t 'to': 'd', 
 
\t \t 'option': '500' 
 
\t } 
 
]; 
 

 
var array2 = []; 
 
for (var a in array) { 
 
\t for (var b in array2) { 
 
\t \t if (array2[b].from == array[a].from && array2[b].to == array[a].to) { 
 
\t \t \t array2[b].option.push(array[a].option); 
 
\t \t \t break; 
 
\t \t } 
 
\t } 
 
\t 
 
\t if (!array2[b] || array2[b].option.indexOf(array[a].option) == -1) { 
 
\t \t array2.push({ 
 
\t \t \t from: array[a].from, 
 
\t \t \t to: array[a].to, 
 
\t \t \t option: [array[a].option] 
 
\t \t }); 
 
\t } 
 
} 
 

 
console.log(array2);

0

您可以使用下面的函數來獲取給定數組中唯一的數組。

var array1 = [ 
{'from':'a','to':'b','option':'100'}, 
{'from':'a','to':'b','option':'200'}, 
{'from':'a','to':'b','option':'300'}, 
{'from':'c','to':'d','option':'400'}, 
{'from':'c','to':'d','option':'500'} 
]; 


function unique(array) { 
    var i = 0, 
     map = {}, // This map object stores the objects of array1 uniquely 
     uniqueArray = [], 
     obj, 
     len = array.length; 

    for (i = 0; i < len; i++) { 
     obj = array[i]; 
     from = obj.from; to = obj.to; 
     // Create an id using from and to of the object 
     id = from + '-' + to; 
     // Check if object is already there in map object 
     if (map[id]) { 
      // If options is not an array then store the options in array 
      map[id].option = map[id].option instanceof Array ? map[id].option : [map[id].option]; 
      map[id].option.push(obj.option); 
     } 
     // If object not available in map then create an object 
     else { 

      map[id] = {}; 
      map[id].from = obj.from; 
      map[id].to = obj.to; 
      map[id].option = obj.option; 
      // Pushing the map object to the unique array 
      uniqueArray.push(map[id]); 
     } 

    } 
    return uniqueArray; 
}