javascript - 传播对象作为函数输入参数

我想将“he”和“she”传入函数 func 并输出“heshe”。

有没有办法传播对象(如数组)的 value 以使其工作?

const func=(a,b)=>(a+b);

  const arr=["he","she"];
  console.log(func(...arr));//working

  const obj1={a:"he", "b":"she"}
  console.log(func(...obj1));//not working

回答1

您需要使用 https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Object/values

在您的示例中:

const func=(a,b)=>(a+b);

const obj1={a:"he", "b":"she"}
console.log(func(...Object.values(obj1)));

相似文章

随机推荐

最新文章