如何解决使用哪种数组方法JS 编辑
const arr = [1,2,3,4,5,6];
让我们说我给它一个条件,使用.filter()方法返回大于3的数字,并返回一个新数组。
但是我需要它来返回原始数组,但要使不满足条件的数组值变为空。
新数组应该为
[,6 ]
这可能吗?
编辑
这是已更改的要求。
const arr = [
{ myNumber: 1,name: 'one' },{ myNumber: 3,name: 'tree' },{ myNumber: 6,name: 'six' },{ myNumber: 8,name: 'eight' }
];
让我们说我给它一个条件,以使用.filter()方法返回包含字母 I 的名称,并返回一个新数组。 但是我需要它来返回原始数组,但要使不满足条件的数组值变为空
新数组应该为
const arr = [
{ myNumber: '',name: '' },{ myNumber: '',name: 'eight' }
];
这可能吗?
解决方法
要映射值并修改现有数组,请使用forEach
方法并修改数组的元素。
const arr = [1,2,3,4,5,6];
arr.forEach((ele,idx) => {
if(ele <= 3) arr[idx] = null;
});
console.log(arr);
,
您可以删除不需要的项目。
const
array = [1,6];
for (let i = 0; i < array.length; i++) if (array[i] <= 3) delete array[i];
console.log(array);
array.forEach((v,i) => console.log(v,i));
.as-console-wrapper { max-height: 100% !important; top: 0; }
,
[,6 ]
是不可能的。但是,[ undefined,undefined,6 ]
和[ null,null,6 ]
也是可能的。
因此,我将使用.filter()
而不是使用map()
:
const arr = [1,6];
const newArr = arr.map((x) => x <= 3 ? undefined : x);
console.log(newArr);
如果您希望进行更改,请使用相同的逻辑,但要定期循环:
const arr = [1,6];
arr.forEach((element,index,original) => {
if (element <= 3)
original[index] = undefined;
});
console.log(arr);
,
自从OP请求使用 数组方法 以来,我经常想推广Array.prototype.reduce
。一次又一次地证明自己是一把优雅的瑞士刀,……但是看到自己……
function deleteConditionMatchingItem(condition,item,idx,arr) {
if (condition(item)) {
// mutate the original array.
delete arr[idx];
}
return condition;
}
function isLowerEqualThanThree(value) {
return (value <= 3);
}
const arr = [1,6];
console.log('before mutation ... arr :',arr);
arr.reduce(deleteConditionMatchingItem,isLowerEqualThanThree);
console.log('after mutation ... arr :',arr);
console.log('("0" in arr ) ? ',("0" in arr ));
console.log('("1" in arr ) ? ',("1" in arr ));
console.log('("2" in arr ) ? ',("2" in arr ));
console.log('("3" in arr ) ? ',("3" in arr ));
console.log('("4" in arr ) ? ',("4" in arr ));
console.log('("5" in arr ) ? ',("5" in arr ));
console.log('("6" in arr ) ? ',("6" in arr ));
.as-console-wrapper { min-height: 100%!important; top: 0; }
,
这个新的答案将证明,即使完全改变了需求,一个通用的想法(通用的基本方法)也可以很容易地适应这些变化,但是仍然可以被认可。
有了新的要求,不需要更改原始数组本身,而是更改它的每个项目(引用)。因此forEach
将成为新的数组选择方法。
以前的要求reduce
and condition
based answer提供了使用condition
等可定制功能的主要思想,并且通过这种方法新引入了通过resetValues
进行物品特定的突变...
function resetConditionMatchingItemValuesViaBoundConfig(item) {
const { condition,resetValues } = this; // `this` equals bound `config`.
if (condition(item)) {
// mutate an original array's item.
resetValues(item);
}
}
function isItemNumberValueLowerEqualThanThree(item) {
return (item.myNumber <= 3);
}
function resetItemValues(item) {
item.myNumber = Number.NaN;
//item.myNumber = '';
item.name = '';
}
const arr = [
{ myNumber: 1,name: 'one' },{ myNumber: 3,name: 'tree' },{ myNumber: 6,name: 'six' },{ myNumber: 8,name: 'eight' }
];
console.log('before mutation ... arr :',arr);
arr.forEach(resetConditionMatchingItemValuesViaBoundConfig,{
condition: isItemNumberValueLowerEqualThanThree,resetValues: resetItemValues,});
console.log('after mutation ... arr :',arr);
.as-console-wrapper { min-height: 100%!important; top: 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。