英文:
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 && 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(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) => 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 => typeof arg === 'object' && !Array.isArray(arg) ? Object.values(arg) : arg)
.flat()
.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 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 && 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}));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论