比较并添加一行代码?
Compare and add in a single line of code?
给定一个数组
[9,5,4,2,1,1,5,8,1,1]
有没有办法删除所有 1
并在末尾添加等量的 x
。
得到这个
[9,5,4,2,5,8,x,x,x,x]
我正在寻找一种在一行中完成此操作的方法。好奇这里是否有我可能遗漏的技术,或者可能有 none.
我在下面的例子中使用 this
显然是错误的。但是让您了解我正在尝试做什么。
let test = [9,5,4,2,1,1,5,8,1,1];
console.log(test.map(el => el !== 1 ?el :this.push('x'));
编辑:正如下面的评论中所指出的,.sort
在此解决方案中的利用方式有点奇怪(大方地说)。我建议查看其他已发布的解决方案以获得更强大的方法。
它不能在一个 operation/loop 中完成(或至少很容易完成),但可以在一行中完成:
let test = [9,5,4,2,1,1,5,8,1,1];
let output = test.map((num) => num === 1 ? 'x' : num).sort((a,b) => b === 'x' ? -1 : 0)
console.log(output);
您对 this
的用法没有意义;在循环操作中,您 return
所需的输出(显式或作为来自胖箭头函数的隐式 return )。
所以在上面的例子中,我们首先使用 .map
循环一次到 return 一个包含所有 1
的新数组s 替换为 "x"
s。然后我们遍历新的 returned 数组到 .sort
"1"
的所有实例到数组的末尾。
使用 filter()
和 fill()
let test = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
let res = test.filter(el => el !== 1)
res = res.concat(Array(test.length - res.length).fill('x'))
console.log(res);
使用reduce()
let test = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
let res = test.reduceRight((a, e) => e !== 1 ? [e, ...a] : [...a, 'x'], [])
console.log(res);
您可以使用两个循环来重新排序数组。
var array = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1],
j = 0;
console.log(...array);
for (let i = 0; i < array.length; i++) {
if (array[i] !== 1) array[j++] = array[i];
}
while (j < array.length) array[j++] = 'x';
console.log(...array);
在 forEach
回调中基本相同。
var array = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
array.forEach((j => (v, i, a) => { if (i + 1 === a.length) while (j < a.length) a[j++] = 'x'; else if (v !== 1) a[j++] = v; })(0));
console.log(...array);
更短的值映射方法
var array = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
array = array.map((j => (v, _, a) => { while (a[j] === 1) j++; return j in a ? a[j++] : 'x'; })(0));
console.log(...array);
关于 this
的使用:您无法从 map
回调中访问 map
正在构建的数组,直到 map
returns吧。
从技术上讲,您可以在一行中使用一个表达式来做到这一点:
console.log(Array.from(Object.assign(test.filter(el => el !== 1), {length: test.length})).map(el => el === undefined ? "x" : el));
假设原文中没有 undefined
个条目,考虑到给定的示例,这似乎是合理的。
实例:
let test = [9,5,4,2,1,1,5,8,1,1];
console.log(Array.from(Object.assign(test.filter(el => el !== 1), {length: test.length})).map(el => el === undefined ? "x" : el));
.as-console-wrapper {
max-height: 100% !important;
}
但是,强烈反对。我只是用简单的方法来做:
const result = test.filter(el => el !== 1);
while (result.length < test.length) {
result.push("x");
}
实例:
let test = [9,5,4,2,1,1,5,8,1,1];
const result = test.filter(el => el !== 1);
while (result.length < test.length) {
result.push("x");
}
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
或者
const result = test.filter(el => el !== 1);
result.push(...Array(test.length - result.length).fill("x"));
实例:
let test = [9,5,4,2,1,1,5,8,1,1];
const result = test.filter(el => el !== 1);
result.push(...Array(test.length - result.length).fill("x"));
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
...但对我来说,即使这样也有点过于复杂了。
给定一个数组
[9,5,4,2,1,1,5,8,1,1]
有没有办法删除所有 1
并在末尾添加等量的 x
。
得到这个
[9,5,4,2,5,8,x,x,x,x]
我正在寻找一种在一行中完成此操作的方法。好奇这里是否有我可能遗漏的技术,或者可能有 none.
我在下面的例子中使用 this
显然是错误的。但是让您了解我正在尝试做什么。
let test = [9,5,4,2,1,1,5,8,1,1];
console.log(test.map(el => el !== 1 ?el :this.push('x'));
编辑:正如下面的评论中所指出的,.sort
在此解决方案中的利用方式有点奇怪(大方地说)。我建议查看其他已发布的解决方案以获得更强大的方法。
它不能在一个 operation/loop 中完成(或至少很容易完成),但可以在一行中完成:
let test = [9,5,4,2,1,1,5,8,1,1];
let output = test.map((num) => num === 1 ? 'x' : num).sort((a,b) => b === 'x' ? -1 : 0)
console.log(output);
您对 this
的用法没有意义;在循环操作中,您 return
所需的输出(显式或作为来自胖箭头函数的隐式 return )。
所以在上面的例子中,我们首先使用 .map
循环一次到 return 一个包含所有 1
的新数组s 替换为 "x"
s。然后我们遍历新的 returned 数组到 .sort
"1"
的所有实例到数组的末尾。
使用 filter()
和 fill()
let test = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
let res = test.filter(el => el !== 1)
res = res.concat(Array(test.length - res.length).fill('x'))
console.log(res);
使用reduce()
let test = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
let res = test.reduceRight((a, e) => e !== 1 ? [e, ...a] : [...a, 'x'], [])
console.log(res);
您可以使用两个循环来重新排序数组。
var array = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1],
j = 0;
console.log(...array);
for (let i = 0; i < array.length; i++) {
if (array[i] !== 1) array[j++] = array[i];
}
while (j < array.length) array[j++] = 'x';
console.log(...array);
在 forEach
回调中基本相同。
var array = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
array.forEach((j => (v, i, a) => { if (i + 1 === a.length) while (j < a.length) a[j++] = 'x'; else if (v !== 1) a[j++] = v; })(0));
console.log(...array);
更短的值映射方法
var array = [9, 5, 4, 2, 1, 1, 5, 8, 1, 1];
array = array.map((j => (v, _, a) => { while (a[j] === 1) j++; return j in a ? a[j++] : 'x'; })(0));
console.log(...array);
关于 this
的使用:您无法从 map
回调中访问 map
正在构建的数组,直到 map
returns吧。
从技术上讲,您可以在一行中使用一个表达式来做到这一点:
console.log(Array.from(Object.assign(test.filter(el => el !== 1), {length: test.length})).map(el => el === undefined ? "x" : el));
假设原文中没有 undefined
个条目,考虑到给定的示例,这似乎是合理的。
实例:
let test = [9,5,4,2,1,1,5,8,1,1];
console.log(Array.from(Object.assign(test.filter(el => el !== 1), {length: test.length})).map(el => el === undefined ? "x" : el));
.as-console-wrapper {
max-height: 100% !important;
}
但是,强烈反对。我只是用简单的方法来做:
const result = test.filter(el => el !== 1);
while (result.length < test.length) {
result.push("x");
}
实例:
let test = [9,5,4,2,1,1,5,8,1,1];
const result = test.filter(el => el !== 1);
while (result.length < test.length) {
result.push("x");
}
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
或者
const result = test.filter(el => el !== 1);
result.push(...Array(test.length - result.length).fill("x"));
实例:
let test = [9,5,4,2,1,1,5,8,1,1];
const result = test.filter(el => el !== 1);
result.push(...Array(test.length - result.length).fill("x"));
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
...但对我来说,即使这样也有点过于复杂了。