如何仅在满足条件时将元素添加到数组
How to add an element to an array only if a condition is fulfilled
有一个创建数组的箭头函数,如:
const myFunction = () => ["a", "b", "c"];
我想为其添加一个参数,如果参数为真,则必须添加另一个元素。
点赞:
const myFunction = (arg) => ["a", "b", "c", arg ? "d" : null];
此解决方案的问题在于,如果 arg !== true
但我不想在这种情况下添加任何内容。
您可以使用数组传播。根据arg
的值一个空数组或一个包含d
的数组将被散布到结果数组中:
const myFunction = (arg) => ["a", "b", "c", ...arg ? ['d'] : []];
console.log(JSON.stringify(myFunction(true))); // ["a","b","c","d"]
console.log(JSON.stringify(myFunction())); // ["a","b","c"]
你可以把函数写长一点,
- 创建一个临时数组,
- 如果需要,将元素附加到临时数组,
- 和return完成后的临时数组
const myFunction = (arg) => {
var tempArray = ["a", "b", "c"];
if (arg) {
tempArray.push("d");
}
return tempArray;
};
console.log(myFunction(true) + "");
console.log(myFunction(false) + "");
您可以使用 concat
:
const myFunction = (arg) => ["a", "b", "c"].concat(arg ? ["d"] : []);
console.log(myFunction(true));
console.log(myFunction(false));
const myFunction = (arg) => {
ret = ['a', 'b', 'c']
return arg === true ? ret.concat('d') : ret;
}
在其他解决方案中,您有 arg ?
而不是 arg === true ?
。
如果你想要 myFunction
到 return 数组,'d'
仅用于 arg = true
,那么你应该使用我的解决方案。如果你想让它 return 'd'
例如 arg = 17
,而不是 return 它 arg = 0
,那么使用其他解决方案。
您也可以这样做:
const myMethod = (arg) => {
var tempArray = ["item 1", "item 2", "item 3"];
!arg || tempArray.push("item 4");
return tempArray;
};
console.log(myMethod(false));
console.log(myMethod(true));
您可以使用 Array push().
const myFunction = (arg) => {
const arr = ["a", "b", "c"];
if (arg) arr.push("d");
return arr;
};
console.log(myFunction(true));
console.log(myFunction(false));
Ori 。将其用于所有现代浏览器。如果出于某种原因您仍然停留在旧版浏览器上 -
["a", "b", "c"].concat(arg ? 'd' : [])
如果您将数组存储在一个变量中,您可以像这样:
const arr = ["a", "b", "c"];
const myFunction = arg => arg === true ? [...arr, "d"] : arr;
console.log(myFunction(true));
console.log(myFunction());
有一个创建数组的箭头函数,如:
const myFunction = () => ["a", "b", "c"];
我想为其添加一个参数,如果参数为真,则必须添加另一个元素。
点赞:
const myFunction = (arg) => ["a", "b", "c", arg ? "d" : null];
此解决方案的问题在于,如果 arg !== true
但我不想在这种情况下添加任何内容。
您可以使用数组传播。根据arg
的值一个空数组或一个包含d
的数组将被散布到结果数组中:
const myFunction = (arg) => ["a", "b", "c", ...arg ? ['d'] : []];
console.log(JSON.stringify(myFunction(true))); // ["a","b","c","d"]
console.log(JSON.stringify(myFunction())); // ["a","b","c"]
你可以把函数写长一点,
- 创建一个临时数组,
- 如果需要,将元素附加到临时数组,
- 和return完成后的临时数组
const myFunction = (arg) => {
var tempArray = ["a", "b", "c"];
if (arg) {
tempArray.push("d");
}
return tempArray;
};
console.log(myFunction(true) + "");
console.log(myFunction(false) + "");
您可以使用 concat
:
const myFunction = (arg) => ["a", "b", "c"].concat(arg ? ["d"] : []);
console.log(myFunction(true));
console.log(myFunction(false));
const myFunction = (arg) => {
ret = ['a', 'b', 'c']
return arg === true ? ret.concat('d') : ret;
}
在其他解决方案中,您有 arg ?
而不是 arg === true ?
。
如果你想要 myFunction
到 return 数组,'d'
仅用于 arg = true
,那么你应该使用我的解决方案。如果你想让它 return 'd'
例如 arg = 17
,而不是 return 它 arg = 0
,那么使用其他解决方案。
您也可以这样做:
const myMethod = (arg) => {
var tempArray = ["item 1", "item 2", "item 3"];
!arg || tempArray.push("item 4");
return tempArray;
};
console.log(myMethod(false));
console.log(myMethod(true));
您可以使用 Array push().
const myFunction = (arg) => {
const arr = ["a", "b", "c"];
if (arg) arr.push("d");
return arr;
};
console.log(myFunction(true));
console.log(myFunction(false));
Ori
["a", "b", "c"].concat(arg ? 'd' : [])
如果您将数组存储在一个变量中,您可以像这样:
const arr = ["a", "b", "c"];
const myFunction = arg => arg === true ? [...arr, "d"] : arr;
console.log(myFunction(true));
console.log(myFunction());