【问题标题】:JS: Merge objects with identical title value into new array objectJS:将具有相同标题值的对象合并到新的数组对象中
【发布时间】:2017-10-29 02:08:36
【问题描述】:

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

'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-field 的“汽车”,第二个集合给我“动物”。

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

{ _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' }
] }

【问题讨论】:

  • 看起来您应该从一开始就以不同的方式设置数据库?

标签: javascript arrays object merge


【解决方案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; }

【讨论】:

  • eslint 给我一个 data.reduce 行的“wrap-iife”错误:eslint.org/docs/rules/wrap-iife我不明白这个...
  • 也许第二种方法确实适合 eslint。
【解决方案2】:

您可以获取每个标题的计数,然后按每个标题使用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)

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2020-02-08
    • 1970-01-01
    • 2022-10-24
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    相关资源
    最近更新 更多