如何测试作为…rest参数传递给函数的对象

huangapple go评论67阅读模式
英文:

How to test for object passed as ...rest argument to a function

问题

我正在练习使用 ...rest 操作符将参数传递给函数。我遇到了一个问题,无法解决,即在将对象传递给函数时。

以下是该函数:

function sum(...args){
  if (args.length === 1 && Array.isArray(args[0])) {
    args = args[0];
  }
  return args.reduce((a,b) => a + b);
}

console.log(sum([5,4,3,2,1])); // 返回 15
console.log(sum(5,4,3,2,1)); // 返回 15
console.log(sum({a:5,b:4,c:3,d:2,e:1})); // 返回 {a:5,b:4,c:3,d:2,e:1}

我不知道如何测试和处理最后一种用例。

英文:

I am practicing using ...rest operator to pass arguments to functions. I have run into an issue that I'm unable to resolve for when passing an object to the function.

Here is the function:

function sum(...args){
  if (args.length === 1 && Array.isArray(args[0])) {
    args = args[0];
  }
  return args.reduce((a,b) => a + b);
}

console.log(sum([5,4,3,2,1])); // Returns 15
console.log(sum(5,4,3,2,1)); // Returns 15
console.log(sum({a:5,b:4,c:3,d:2,e:1})); // Returns {a:5,b:4,c:3,d:2,e:1}

I'm stuck at how to test for and handle the last use case.

答案1

得分: 0

Convert your object to an array with Object.values, passing an object instead of an array is an overkill:

function sum(...args){
  if (args.length === 1 && Array.isArray(args[0])) {
    args = args[0];
  }
  return args.reduce((a,b) => a + b);
}

console.log(sum([5,4,3,2,1])); // 返回 15
console.log(sum(5,4,3,2,1)); // 返回 15
console.log(sum(Object.values({a:5,b:4,c:3,d:2,e:1}))) // 返回

Also you could possibly improve your function with Array::flat(). That way it will behave the same as Array::concat(). I like that.

function sum(...args){
  return args.flat().reduce((a,b) => a + b);
}

console.log(sum([5,4,3,2,1])); // 返回 15
console.log(sum(5,4,3,2,1)); // 返回 15
console.log(sum(Object.values({a:5,b:4,c:3,d:2,e:1}))) // 返回 15
console.log(sum(
  [5,4,3,2,1],
  5,4,3,2,1,
  Object.values({a:5,b:4,c:3,d:2,e:1})
)); // 返回 45

I wouldn't convert an object to an array automatically inside sum but if you need:

function sum(...args){
  return args
    .map(arg => typeof arg === 'object' && !Array.isArray(arg) ? Object.values(arg) : arg)
    .flat()
    .reduce((a,b) => a + b);
}

console.log(sum([5,4,3,2,1])); // 返回 15
console.log(sum(5,4,3,2,1)); // 返回 15
console.log(sum({a:5,b:4,c:3,d:2,e:1})) // 返回 15
console.log(sum(
  [5,4,3,2,1],
  5,4,3,2,1,
  {a:5,b:4,c:3,d:2,e:1}
)); // 返回 45
英文:

Convert your object to an array with Object.values, passing an object instead of an array is an overkill:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function sum(...args){
  if (args.length === 1 &amp;&amp; Array.isArray(args[0])) {
    args = args[0];
  }
  return args.reduce((a,b) =&gt; a + b);
}

console.log(sum([5,4,3,2,1])); // Returns 15
console.log(sum(5,4,3,2,1)); // Returns 15
console.log(sum(Object.values({a:5,b:4,c:3,d:2,e:1}))) //

<!-- end snippet -->

Also you could possibly improve your function with Array::flat().
That way it will behave the same as Array::concat(). I like that.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function sum(...args){
  return args.flat().reduce((a,b) =&gt; a + b);
}

console.log(sum([5,4,3,2,1])); // Returns 15
console.log(sum(5,4,3,2,1)); // Returns 15
console.log(sum(Object.values({a:5,b:4,c:3,d:2,e:1}))) // Returns 15
console.log(sum(
  [5,4,3,2,1],
  5,4,3,2,1,
  Object.values({a:5,b:4,c:3,d:2,e:1})
)); // Returns 45

<!-- end snippet -->

I wouldn't convert an object to an array automatically inside sum but if you need:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function sum(...args){
  return args
    .map(arg =&gt; typeof arg === &#39;object&#39; &amp;&amp; !Array.isArray(arg) ? Object.values(arg) : arg)
    .flat()
    .reduce((a,b) =&gt; a + b);
}

console.log(sum([5,4,3,2,1])); // Returns 15
console.log(sum(5,4,3,2,1)); // Returns 15
console.log(sum({a:5,b:4,c:3,d:2,e:1})) // Returns 15
console.log(sum(
  [5,4,3,2,1],
  5,4,3,2,1,
  {a:5,b:4,c:3,d:2,e:1}
)); // Returns 45

<!-- end snippet -->

答案2

得分: -1

你可以在两种情况下都使用 Object.values(对象和数组):

function sum(...args){
  if (args.length === 1 && args[0] && typeof args[0] === 'object') {
    args = Object.values(args[0]);
  }
  return args.reduce((a,b) => a + b);
}

console.log(sum([5,4,3,2,1]));
console.log(sum(5,4,3,2,1));
console.log(sum({a:5,b:4,c:3,d:2,e:1}));
英文:

You could use Object.values for both cases (object and array):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function sum(...args){
  if (args.length === 1 &amp;&amp; args[0] &amp;&amp; typeof args[0] === &#39;object&#39;) {
    args = Object.values(args[0]);
  }
  return args.reduce((a,b) =&gt; a + b);
}

console.log(sum([5,4,3,2,1]));
console.log(sum(5,4,3,2,1));
console.log(sum({a:5,b:4,c:3,d:2,e:1}));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 01:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定