Javascript 在数组上使用array.prototype.map/filter后,是否可以将其转换回原始数组?
我正在研究FreeCodeCamp算法,该算法要求我执行以下操作: 从数组中删除所有错误值。Javascript 在数组上使用array.prototype.map/filter后,是否可以将其转换回原始数组?,javascript,arrays,Javascript,Arrays,我正在研究FreeCodeCamp算法,该算法要求我执行以下操作: 从数组中删除所有错误值。 function bouncer(arr) { // First thing I did was change the arr argument into an array that changed all the values into true or false. var newArr = arr.map(function(elements) { return Boolean(ele
function bouncer(arr) {
// First thing I did was change the arr argument into an array that changed all the values into true or false.
var newArr = arr.map(function(elements) {
return Boolean(elements);
});
// Now that all the elements are easily filterable, I did that exactly.
var filterArr = newArr.filter(function(value) {
return value === true;
/*Freecodecamp keeps telling me to use '==' here instead of '==='. I know the difference between
the strict equality operator and the equality operator, but wouldn't '==' work just as well?
*/
});
/* Now filterArr outputs [true, true, true].
Is there anyway to somehow reverse the map method I used earlier
so that it 'converts' the [true, true, true] array into [7, "ate", 9] as the output?
*/
return filterArr;
}
bouncer([7, "ate", "", false, 9]);
JavaScript中的Falsy值为false、null、0、“、未定义和NaN。
function bouncer(arr) {
// First thing I did was change the arr argument into an array that changed all the values into true or false.
var newArr = arr.map(function(elements) {
return Boolean(elements);
});
// Now that all the elements are easily filterable, I did that exactly.
var filterArr = newArr.filter(function(value) {
return value === true;
/*Freecodecamp keeps telling me to use '==' here instead of '==='. I know the difference between
the strict equality operator and the equality operator, but wouldn't '==' work just as well?
*/
});
/* Now filterArr outputs [true, true, true].
Is there anyway to somehow reverse the map method I used earlier
so that it 'converts' the [true, true, true] array into [7, "ate", 9] as the output?
*/
return filterArr;
}
bouncer([7, "ate", "", false, 9]);
因此,虽然我设法删除了falsy值,但我不知道如何将过滤后的数组分别转换回原始数组。我想自己解决这个问题,但我有点卡住了
如果还有别的方法我应该用的话
如果代码看起来像垃圾,我会道歉。我试着用评论来解释我的思路和问题 我觉得这很容易,对吧
功能弹跳器(arr){
返回arr.filter(布尔值);
}
log(bouncer([7,“ate”,“”,false,9,0,NaN,true,无穷大,null,未定义,{}])代码>我认为这很容易,对吗
功能弹跳器(arr){
返回arr.filter(布尔值);
}
log(bouncer([7,“ate”,“”,false,9,0,NaN,true,无穷大,null,未定义,{}])
只需过滤您得到的数组,这取决于.filter
将回调结果作为布尔值:
function bouncer(arr) {
return arr.filter(function(value) {
return value;
});
}
或者使用ES6语法:
let bouncer = (arr) => arr.filter(v => v);
只需根据.filter
将回调结果作为布尔值的事实,对已获得的数组进行筛选:
function bouncer(arr) {
return arr.filter(function(value) {
return value;
});
}
或者使用ES6语法:
let bouncer = (arr) => arr.filter(v => v);
既然已经给出了建议使用filter()
的好答案,我将为您提供一个解决方案,用于您何时真正想要修改原始数组,这是不寻常的,最好避免,但仍然有有效的用例:
function bouncer(arr) {
var i = 0;
while (i < arr.length) {
if (arr[i]) {
i++;
} else {
arr.splice(i, 1);
}
}
}
功能弹跳器(arr){
var i=0;
而(i
既然已经给出了建议使用filter()
的好答案,我将提供一个解决方案,用于您何时真正想要修改原始数组,这是不寻常的,最好避免,但仍然有有效的用例:
function bouncer(arr) {
var i = 0;
while (i < arr.length) {
if (arr[i]) {
i++;
} else {
arr.splice(i, 1);
}
}
}
功能弹跳器(arr){
var i=0;
而(i
不,当然不可能,您已经销毁了这些信息。您只需执行返回arr.filter(el=>el)
filter()
和map()
即可返回一个新数组,它们不会以任何方式影响原始数组,因此原始数组,arr
是完整的。我不明白你到底想做什么,你想过滤阵列只是为了将其恢复到原始状态?重点是什么?原始状态减去falsy值@米凯伦·霍尔姆诺,当然不可能——你已经破坏了这些信息。您只需执行返回arr.filter(el=>el)filter()
和map()
即可返回一个新数组,它们不会以任何方式影响原始数组,因此原始数组,arr
是完整的。我不明白你到底想做什么,你想过滤阵列只是为了将其恢复到原始状态?重点是什么?原始状态减去falsy值@我不知道我在想什么!你的回答更有意义!我不知道我在想什么!你的回答更有意义!无需转换为布尔值
,.filter
函数会隐式地将其转换为回调的返回值value@Alnitak是的,我知道,但是对于初学者来说,返回元素本身的意图可能并不明显,因此,将其转换为Boolean
会引起人们的注意,它的真实性决定了filter()
的动作。但这给了我一个想法,看我的编辑。它在documentation@Alnitak“RTFM”并不是掩盖意图的借口,因为它很容易显示。不需要转换为布尔值
,.filter
函数会隐式地将其转换为回调的返回值value@Alnitak是的,我知道,但是对于初学者来说,返回元素本身的意图可能并不明显,因此将其转换为Boolean
会引起注意,它的真实性决定了filter()
的操作。但这给了我一个想法,看我的编辑。它在documentation@Alnitak“RTFM”并不是掩盖意图的借口,因为它很容易显示。但是,甚至不要在JS解释器上运行它,因为Array.prototype.splice
是一种O(n)
操作…:p但甚至不要在JS解释器上运行此操作,因为Array.prototype.splice
是一个O(n)
操作…:P