2017-10-20 156 views
0

我正在嘗試使用相同的鍵將項目內的嵌套對象組合在一起。組合匹配的對象數組

  • 查找「頂層」是重複的值,
  • 合併重複的「頂級」項目到一個對象(包括他們的孩子。
  • 應該有「類型」列中沒有重複的值

我想在這裏https://jsfiddle.net/Lpq6huvw/410/

輸入數據:

[{ 
    "a": "Mon", 
    "type": [{ 
     "b": 1 
    }, { 
     "b": 3 
    }] 
}, { 
    "a": "Mon", 
    "type": [{ 
     "b": 2 
    }] 
}, { 
    "a": "Tue", 
    "type": [{ 
     "b": 40 
    }] 
}, { 
    "a": "Tue", 
    "type": [{ 
     "b": 50 
    }] 
}, { 
    "a": "Wed", 
    "type": [{ 
     "b": 30 
    }] 
}] 

這個數組:

[{ 
     "a": "Mon", 
     "type": [{ 
     "b": 1 
     }, { 
     "b": 3 
     }, 
     { 
     "b": 2 
     }] 
    }, 
    { 
     "a": "Tue", 
     "type": [{ 
     "b": 40 
     }, 
     { 
     "b": 50 
     }] 
    }, { 
     "a": "Wed", 
     "type": [{ 
     "b": 30 
     }] 
    }] 

我嘗試這下面,所有的重複項目的映射爲一個對象。但是,我希望它將每個映射到它的「頂級」前任。

const z = _.uniqBy(_.filter(data.map(e=>e.a), v => _.filter(data.map(e=>e.a), v1 => v1 === v).length > 1)) 
const dupes = data.filter(itm => z.includes(itm.a)) 

const flat = _.flatMap(dupes, item => 
         _(item.type) 
          .map(v => ({b: v.b})) 
          .value() 
) 
+0

將屬性名稱始終是相同的,只值變化? – nnnnnn

+0

是的,他們將保持不變 – Ycon

+1

*「在'類型'數組內不應該有重複的值」* - 你的意思是說,輸入將永遠不會有'類型'值的重複,或者輸入可能有重複,但輸出不應該? – nnnnnn

回答

2

我個人覺得JavaScript的內置函數的工作不錯,而且似乎更容易執行比某些lodash功能。

例如。

var data = [{"a":"Mon","type":[{"b":1},{"b":3}]},{"a":"Mon","type":[{"b":2},{"b":3}]},{"a":"Tue","type":[{"b":40}]},{"a":"Tue","type":[{"b":50}]},{"a":"Wed","type":[{"b":30}]}]; 
 

 
    
 
var result = data.reduce((acc, val) => { 
 
    var found = acc.find((findval) => val.a === findval.a); 
 
    if (!found) acc.push(val) 
 
    else found.type = found.type.concat(
 
    val.type.filter((f) => !found.type.find((findval) => f.b === findval.b))); 
 
    return acc; 
 
}, []); 
 
    
 
console.log(result);

+0

偉大的最小實現。只是有點困惑,爲什麼數據被命名爲'var a',然後返回'a'。無論哪種方式 - 它的作品! – Ycon

+0

'a'在返回時是accumulator中的reduce函數。我會重新命名一些增值稅以防止混淆。 – Keith

+0

僅供參考我將其與另一個問題的解決方案相結合 - https://stackoverflow.com/questions/46845461 – Ycon

1

這裏有一個答案W/O lodash:

function combine (input) { 
    const hash = input.reduce((result, current) => { 
    if (result[current['a']]) { 
     result[current['a']] = result[current['a']].concat(current['type']) 
    } else { 
     result[current['a']] = current['type'] 
    } 

    return result 
    }, {}) 

    return Object.keys(hash).map(key => { 
    return { 
     a: key, 
     type: hash[key] 
    } 
    }) 
} 
1

ES6:你可以使用Array#迭代減少,收集物品放入一個地圖,然後轉換回與數組傳播語法和地圖#值:

const data = [{"a":"Mon","type":[{"b":1},{"b":3}]},{"a":"Mon","type":[{"b":2}]},{"a":"Tue","type":[{"b":40}]},{"a":"Tue","type":[{"b":50}]},{"a":"Wed","type":[{"b":30}]}]; 
 

 
const result = [...data.reduce((m, { a, type }) => { 
 
    const item = m.get(a) || { a, type: [] }; // use a Set to maintain uniqueness 
 
    
 
    item.type.push(...type); 
 
    
 
    return m.set(a, item); 
 
}, new Map).values()] 
 
.map(({ a, type }) => ({ // make types unique again 
 
    a, 
 
    type: [...type.reduce((m, o) => m.has(o.b) ? m : m.set(o.b, o), new Map).values()] 
 
})); 
 

 

 
console.log(result);

Lodash:使用_.groupBy()可以獲得在同一組中具有相同a屬性的所有對象。映射組,並使用_.mergeWith()合併每個組,並將所有type數組連接起來。 再次使用地圖來傳遞type數組中的所有項目。

const data = [{"a":"Mon","type":[{"b":1},{"b":3}]},{"a":"Mon","type":[{"b":2}]},{"a":"Tue","type":[{"b":40}]},{"a":"Tue","type":[{"b":50}]},{"a":"Wed","type":[{"b":30}]}]; 
 

 

 
const result = _(data) 
 
    .groupBy('a') 
 
    .map((group) => _.mergeWith({}, ...group, ((objValue, srcValue, key) => 
 
    key === 'type' ? (objValue || []).concat(srcValue) : undefined 
 
))) 
 
    .map((obj) => Object.assign(obj, { type: _.uniq(obj.type) })) 
 
    .value(); 
 

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

+0

Thansk爲那些 - 但似乎都不顯示在'b'屬性內的多個項目。你能塞住嗎? – Ycon

+0

lodash是獨一無二的。我已經修復了ES6。 –