如何合并两个JavaScript对象的属性,而不喜欢不使用null值?

时间:2018-07-18 14:34:48

标签: javascript

这是两个对象:

const obj1 = {a: null, b: "b"} 
const obj2 = {a: "a", b: null}

如何合并两个对象并获得以下对象?

{a: "a", b: "b"}

我可以这样做:

const merged = {...obj1, ...obj2}

但是它返回此:

{ a: "a", b: null }

是否有一种方法可以合并两个对象,而又不希望使用null(也不是空值,undefined等)值?

9 个答案:

答案 0 :(得分:3)

function merge(obj1, obj2) {
  answer = {}
  for(key in obj1) {
    if(answer[key] === undefined || answer[key] === null)
      answer[key] = obj1[key];
  }
  for(key in obj2) {
    if(answer[key] === undefined || answer[key] === null)
      answer[key] = obj2[key];
  }
  return answer
}

答案 1 :(得分:0)

if(e.target !== e.currentTarget) return;

答案 2 :(得分:0)

AutoMapper

这将两个对象合并成一个循环

答案 3 :(得分:0)

尝试一下。

const obj1 = {a: null, b: "b"} 
const obj2 = {a: "a", b: null}
const obj3 = {}
for (var k in obj1) {
  obj3[k] = obj1[k] ? obj1[k] : obj2[k];
}
console.log(obj3);

答案 4 :(得分:0)

在对象forEach()上进行简单的key循环怎么样?它将同时适用于nullundefined值:

const obj1 = {a: null, b: "b"};
const obj2 = {a: "a", b: null};
const merged = {};
Object.keys(obj1).forEach((key) => merged[[key]] = obj1[key] ? obj1[key] : obj2[key]);
console.log(merged);

答案 5 :(得分:0)

您可以创建一个函数,该函数获取对象数组作为参数。

这样,无论您有多少个对象,都将得到合并在一起的结果,不包括未定义和空值。只需将它们作为数组发送即可。

您可以在其中传递所有对象,对其进行映射,然后使用for (const [key, value] of Object.entries(obj))遍历它们的键和值,并排除undefinednull的值

见下文

const obj1 = {
  a: null,
  b: "goodb",
  c: 0,
}
const obj2 = {
  a: "gooda",
  b: null,
  c: undefined
}

function cleanObjects(arr) {
  let o = {}
  arr.map((obj) => {
    for (const [key, value] of Object.entries(obj)) {
      typeof value === 'undefined' || value === null ?
      	delete obj[key] : o[key] = value;
    }
  })
  return o;
}
const result = cleanObjects([obj1, obj2])
console.log(result)

答案 6 :(得分:0)

这是已接受答案的修改版本:

function merge(obj1, obj2) {
  let merged = { ...obj1 }

  for (key in obj2) {
    if (merged[key] === undefined || merged[key] === null)
      merged[key] = obj2[key];
  }
  return merged
}

答案 7 :(得分:0)

我建议为此使用 Lodash mergeWith

const obj1 = {a: null, b: "b"} 
const obj2 = {a: "a", b: null}

const result = _.mergeWith({}, obj1, obj2,
  (a, b) => b === null ? a : undefined
)

// result: {a: "a", b: "b"}

答案 8 :(得分:0)

您可以扩展 Javascript ObjectConstructor 本身。添加函数merge, 这会覆盖下一个非空值并将所有对象的属性添加到一个对象中。

/* @/utils.js */

Object.merge = function (...objs) {
  const obj = {};
  objs.reduce((prevObj, currentObj) => {
    if (typeof prevObj === 'object' && typeof currentObj === 'object') Object.entries(currentObj).forEach(([k, v]) => {
      obj[k] = v === null || v === undefined
        ? prevObj[k]
        : v;
    });
    return obj;
  }, {});
  return obj;
};

/* @/app.js */

Object.merge(a,b,c,...z);

Object.merge = function (...objs) {
  const obj = {};
  objs.reduce((prevObj, currentObj) => {
    if (typeof prevObj === 'object' && typeof currentObj === 'object') Object.entries(currentObj).forEach(([k, v]) => {
      obj[k] = v === null || v === undefined
        ? prevObj[k]
        : v;
    });
    return obj;
  }, {});
  return obj;
}

const john = {
name: 'John Doe',
age: 40,
heigth: '5.3ft'
}

const jane = {
name: 'Jane Doe',
age: null,
heigth: '4.1ft'
}

const mark = {
name: 'Mark',
age: 35,
heigth: null
}

const ghost = {
name: null,
age: null,
heigth: null,
planet: 'unknown'
}

const noname = {
name: null,
age: 100,
heigth: '100ft',
planet: '100M-E'
}

console.log(Object.merge(john,jane,mark,ghost,noname))