如何检查通过rest参数传递的对象是否为null或undefined?

4

function extend(...args),如果我运行这个函数并传入一个空对象

function extend({}),如何检查它是否为null或undefined。

输入:const first = { x: 2, y: 3}; const second = { a: 70, x: 4, z: 5 }; const third = { x: 0, y: 9, q: 10 }; const firstSecondThird = extend(first, second, third);

期望输出:{ x: 2, y: 3, a: 70, z: 5, q: 10 } 我还必须检查每个...args中的条目是否为对象且不为undefined;如果有任何未定义的对象,则必须抛出错误。我还必须检查是否至少有两个参数。


现在检查一下解决方案,告诉我哪里出了问题。 - Maheer Ali
由于您没有颠倒对象,它们被后面的参数所替换,因此不能提供预期输出...抛出的错误应基于firstSecondThird而不是传入的参数。 - Varun Suresh Kumar
2个回答

2
  • 你可以使用 Object.keyslength 属性来检查一个对象 {} 是否为空,因为 Boolean({}) 的值为 true
  • 使用 includes() 来检查 args 是否包含 nullundefined
  • 使用 some()typeof 来检查数组中所有元素是否为 object
  • 如果你不希望属性被覆盖,可以使用 reverse()

function extend(...args){
  if(args.length < 2) throw ("Length is less than 2");
  if(!args.some(arg => typeof arg !== "object" || Object.keys(arg).length >=2 )) throw ("Arguments contain something other than object")
  if(args.includes(undefined) || args.includes(null)) throw ("Arguments contain null or undefined");
  else return Object.assign(...args.reverse()) 
   
}
const first = { x: 2, y: 3}; 
const second = { a: 70, x: 4, z: 5 };  
const third = { x: 0, y: 9, q: 10 };  
const firstSecondThird = extend(first, second, third);
console.log(firstSecondThird)


console.log(extend({}));
console.log(extend(1,2,3, undefined));
console.log(extend(1,2,3, null));
console.log(extend([]));

以上所有的if语句可以合并成一条语句。最初的回答。

我的函数使用Object.assign(...args)方法来创建一个新的目标对象,该对象使用传递的对象中的值。问题是,每次执行函数时,在运行代码以检查空值时,它会给出一个TypeError:无法将未定义或空值转换为对象。有可能的解决方案吗? - Vedant Soni
@Vedant Soni,请尝试使用Object.assign(args)。请展示您的函数。 - Maheer Ali
@VSoni,您想从“args”中删除“null”和“undefined”吗? - Maheer Ali
我的函数从传递的所有对象条目中组合一个新对象,而不会覆盖先前条目的属性。如果我使用Object.assign(args),函数将无法按照预期工作。 - Vedant Soni
@VSoni,请在原帖中提供您的输入和期望输出。您的问题有点简短。 - Maheer Ali
输入:const first = { x: 2, y: 3}; const second = { a: 70, x: 4, z: 5 }; const third = { x: 0, y: 9, q: 10 }; const firstSecondThird = extend(first, second, third); 预期输出:{ x: 2, y: 3, a: 70, z: 5, q: 10 }。我还必须检查...args中的每个条目是否为对象而不是未定义;如果有任何未定义,则必须抛出错误。我还必须检查是否至少有2个参数。有什么解决方案吗?@MaheerAli - Vedant Soni

-1

你可以实现这个来得到你期望的输出结果。

const first = { x: 2, y: 3 }; 
const second = { a: 70, x: 4, z: 5 };  
const third = { x: 0, y: 9, q: 10 };
const fourth = undefined;

const firstSecondThird = extend(first, second, third, fourth);

function extend (...args) {
    let result = Object.assign({}, ...args.reverse()); 
    if (result.length < 2) throw ("Length is less than 2");
    if (args.includes(undefined) || args.includes(null)) throw ("args contains undefined")
    return result;
}

console.log(firstSecondThird)

const first = { x: 2, y: 3 }; 
const second = { a: 70, x: 4, z: 5 };  
const third = { x: 0, y: 9, q: 10 };

const firstSecondThird = extend(first, second, third);

function extend (...args) {
    let result = Object.assign({}, ...args.reverse()); 
    if (result.length < 2) throw ("Length is less than 2");
    if (args.includes(undefined) || args.includes(null)) throw ("args contains undefined")
    return result;
}

console.log(firstSecondThird)


伙计,你的代码没有像 OP 说的那样抛出任何错误:“我还必须检查...args中的每个条目是否为对象而不是未定义;如果有任何未定义的条目,则必须抛出错误。” - Maheer Ali
这是已接受答案的精确副本。 - Raydot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接