2017-10-20 124 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。 –