JS:将具有相同标题值的对象合并到新数组对象中

时间:2017-05-28 19:14:04

标签: javascript arrays object merge

我试图从不同的集合中获取一些数据并将它们作为一个对象数组返回,但我需要合并具有相同标题的对象。

'search': function(value) {
    const res1 = Col1.find({ title: new RegExp(value, 'i') }, { fields: { title: 1, type: 1 } }).fetch() || [],
          res2 = Col2.find({ title: new RegExp(value, 'i') }, { fields: { title: 1, type: 1 } }).fetch() || []

    let result = res1.concat(res2)

    return result
}

假设第一个系列会给我“汽车”。对于type - 字段,第二个给我'动物'。

如果标题相同,我需要更改type - 字段并添加一些内容。

{ _id: 1, title: 'mercedes', type: 'cars' }
{ _id: 2, title: 'monkey', type: 'animals' }
{ _id: 3, title: 'jaguar', type: 'cars' }
{ _id: 4, title: 'jaguar', type: 'animals' }

应转换为

{ _id: 1, title: 'mercedes', type: 'cars' }
{ _id: 2, title: 'monkey', type: 'animals' }
{ title: 'jaguar', type: 'multiple', results: [
    { _id: 3, title: 'jaguar', type: 'cars' }
    { _id: 4, title: 'jaguar', type: 'animals' }
] }

2 个答案:

答案 0 :(得分:1)

您可以对相同的标题使用哈希表titles,并将项目分配给数组并保存索引。

var data = [{ _id: 1, title: 'mercedes', type: 'cars' }, { _id: 2, title: 'monkey', type: 'animals' }, { _id: 3, title: 'jaguar', type: 'cars' }, { _id: 4, title: 'jaguar', type: 'animals' }],
    result = data.reduce(function (titles) {
        return function (r, a) {
            if (titles[a.title]) {
                if (titles[a.title].results.length === 1) {
                    r[titles[a.title].index] = { title: a.title, type: 'multiple', results: titles[a.title].results };
                }
                titles[a.title].results.push(a);
            } else {
                titles[a.title] = { index: r.push(a) - 1, results: [a] };
            }
            return r;
        };
    }(Object.create(null)), []);

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

您可以将哈希表移到闭包之外。

var data = [{ _id: 1, title: 'mercedes', type: 'cars' }, { _id: 2, title: 'monkey', type: 'animals' }, { _id: 3, title: 'jaguar', type: 'cars' }, { _id: 4, title: 'jaguar', type: 'animals' }],
    titles = Object.create(null),
    result = data.reduce(function (r, a) {
        if (titles[a.title]) {
            if (titles[a.title].results.length === 1) {
                r[titles[a.title].index] = { title: a.title, type: 'multiple', results: titles[a.title].results };
            }
            titles[a.title].results.push(a);
        } else {
            titles[a.title] = { index: r.push(a) - 1, results: [a] };
        }
        return r;
    }, []);

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

答案 1 :(得分:0)

您可以计算每个标题的数量,然后使用每个标题filter创建一个新数组:

const a = [
  { _id: 1, title: 'mercedes', type: 'cars' },
  { _id: 2, title: 'monkey', type: 'animals' },
  { _id: 3, title: 'jaguar', type: 'cars' },
  { _id: 4, title: 'jaguar', type: 'animals' }
],
  titles = [...new Set(a.map(({title}) => title))] // count for each title
    .map(t => Object.assign({[t]: a.filter(({title}) => title === t).length})) // current title count
    .reduce((acc, v) => Object.assign(acc, v), {}); // form an object for convenience
    
const res = Object.keys(titles) // for each title
  .reduce((acc, t) => (titles[t] === 1) // if not multiple
    ? acc.concat(a.filter(({title}) => title === t)) // add the element
    : acc.concat(Object.assign({title: t, type: 'multiple', // form 'multiple' element
    	results: a.filter(({title}) => title === t)}))
  , [])
  
console.log(res)