【问题标题】:Merge objects containg array of objects合并包含对象数组的对象
【发布时间】:2019-07-23 09:36:45
【问题描述】:

我有两个深度嵌套的对象,其中包含数组和这些数组中的对象。我想合并这些。使用 'lodash' 它无法识别我在数组中的对象。

我有这个:

var defaults = {
    rooms: [
        {
            name: 'livingroom',
            color: 'white'
        },
        {
            name: 'bedroom',
            color: 'red'
        }
    ],
    people: {
        clothes: [
            {
                fabric: 'wool',
                color: 'black'
            },
            {
                fabric: 'jeans',
                color: 'blue'
            }
        ]
    }
}

var specific = {
    people: {
        wigs: [
            {
                shape: 'trump'
            }
        ]
    },
    furniture : {
        foo: 'bar'
    }
}

结果应该是这样的:

var merged = {
    rooms: [
        {
            name: 'livingroom',
            color: 'white'
        },
        {
            name: 'bedroom',
            color: 'red'
        }
    ],
    people: {
        clothes: [
            {
                fabric: 'wool',
                color: 'black'
            },
            {
                fabric: 'jeans',
                color: 'blue'
            }
        ],
        wigs: [
            {
                shape: 'trump'
            }
        ]
    },
    furniture : {
        foo: 'bar'
    }
}

使用 lodash

const _ = require('lodash');
console.log(_.merge({}, specific, defaults));

我明白了

{ people: { wigs: [ [Object] ], clothes: [ [Object], [Object] ] },
  furniture: { foo: 'bar' },
  rooms:
   [ { name: 'livingroom', color: 'white' },
     { name: 'bedroom', color: 'red' } ] }

这与:

Merge Array of Objects by Property using Lodash

How to merge two arrays in JavaScript and de-duplicate items

因为我没有共同的索引或名称,所以我有点迷路了。

【问题讨论】:

  • 你需要保留对象,还是可以改变对象?
  • 不需要保留它。突变很好。
  • Lodash 的 _.merge() 适用于您的情况 - jsfiddle.net/py4h830d。查看浏览器的控制台。
  • OriDrori 你是对的。我还是 js 新手,并且(仅)在 console.log() 上有问题 :-) 但是我喜欢 Nina Scholz 的答案,因为它不需要 lodash。如果我接受她的回答,我希望没关系:)

标签: javascript arrays object lodash


【解决方案1】:

您可以通过查看对象或数组将specific 合并到defaults。

function merge(a, b) {
    Object.keys(b).forEach(k => {
        if (!(k in a)) {
            a[k] = b[k];
            return;
        }
        if ([a, b].every(o => o[k] && typeof o[k] === 'object' && !Array.isArray(o[k]))) {
            merge(a[k], b[k]);
            return;
        }
        if (!Array.isArray(a[k])) a[k] = [a[k]];
        a[k] = a[k].concat(b[k]);
    });
    return a;
}

var defaults = { rooms: [{ name: 'livingroom', color: 'white' }, { name: 'bedroom', color: 'red' }], people: { clothes: [{ fabric: 'wool', color: 'black' }, { fabric: 'jeans', color: 'blue' }] } },
    specific = { people: { wigs: [{ shape: 'trump' }] }, furniture: { foo: 'bar' } };

[defaults, specific].reduce(merge);

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

【讨论】:

  • IIUC,这不会合并数字和字符串 no 等原始属性吗?
  • 它生成一个数组并推送一个值。
  • 哦,你是对的。我认为a[k].push(k) 应该是a[k].push(b[k])。此外,可能需要单独检查数组。如果两个对象都有rooms,那么它将成为一个二维数组,因为push
  • 我认为应该这样做:a[k] = a[k].concat(b[k]);
  • 谢谢 Nina 和 adiga - 美丽 :)
猜你喜欢
  • 2017-01-16
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多