Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 筛选对象数组_Javascript_Node.js - Fatal编程技术网

Javascript 筛选对象数组

Javascript 筛选对象数组,javascript,node.js,Javascript,Node.js,我正在尝试检查数组中的一个对象的id是否为2,如果是,请删除该对象list.filter(e=>e.id==2)返回[{name:'bread',id:2}]这是我想要删除的部分,但是如果我通过执行if(list.indexOf(list.filter(e=>e.id==2))!=-1来检查它是否在数组中,则返回-1,表示它不在列表中。任何帮助都将不胜感激 var list = new Array(); list.push({name: 'apple', id: 1}) list.push({n

我正在尝试检查数组中的一个对象的id是否为2,如果是,请删除该对象
list.filter(e=>e.id==2)
返回
[{name:'bread',id:2}]
这是我想要删除的部分,但是如果我通过执行
if(list.indexOf(list.filter(e=>e.id==2))!=-1来检查它是否在数组中,则返回-1,表示它不在列表中。任何帮助都将不胜感激

var list = new Array();
list.push({name: 'apple', id: 1})
list.push({name: 'bread', id: 2})
console.log(list.filter(e => e.id === 2));
console.log(list);
if(list.indexOf(list.filter(e => e.id === 2)) != -1) {
    list.splice(list.indexOf(list.filter(e => e.name === 2)));
    console.log(list);
} else {
    console.log('The id of 2 has not been found');
}

您正在使用过滤器,就像它过滤掉元素一样,而实际上它会保留符合条件的元素。将您的条件更改为
e.id!==2
并且它使ID不等于2的所有元素:

var list=new Array();
list.push({name:'apple',id:1})
list.push({name:'bread',id:2})
list.push({name:'milk',id:3})
list.push({name:'butter',id:4})
console.log(list.filter(e=>e.id!==2))

。作为控制台包装{max height:100%!important;top:0;}
您需要反转
过滤器
谓词函数的逻辑

list.filter(e => e.id !== 2);

filter
只返回与谓词匹配的项-在您的示例中,它将只返回一个ID为2的元素列表

使用
indexOf
测试您正在搜索包含已找到元素的数组

filter
返回包含结果的数组。您应该使用
find
,它返回单个元素:

var list=new Array();
list.push({name:'apple',id:1})
list.push({name:'bread',id:2})
var index=list.indexOf(list.find(e=>e.id==2));
var结果=索引!==-1.
console.log('index',index);

console.log('result',result)然后使用
==改为
==

但您可以使用
find
方法

var elem = list.find(e => e.id === 2);
if(elem)
   list = list.filter(e => e.id !== 2);
else
   console.log('The id of 2 has not been found');
当您使用list.filter(e=>e.name==2)时。它将返回一个数组包含对象,而不是对象本身。因此它将返回-1。可以使用排列语法从包含对象的数组中提取对象:

list.indexOf(...list.filter(e => e.id === 2))

indexOf
将不起作用,因为您将搜索一个对象,而JavaScript中的对象比较很棘手(需要一个自定义计算器来检查相同性)。好消息是你可能不需要它

正如评论和其他地方所指出的,过滤器的行为与您所期望的相反。将比较运算符从
==
更改为
==是解决该问题的一种方法

在下面的代码中,我包含了一些您可能会发现有价值的其他商品,例如:

  • Array.of()
  • 速记属性名称(例如,
    {foo}===={'foo':foo}
  • 使用逗号运算符(即
    )执行序列,尤其是为了避免大括号
  • 作为函数参数的rest运算符
  • 及其他
let search_id=2;//要过滤掉的id
let list=Array.of(
{name:'apple',id:1},
{name:'bread',id:2},
{name:'orange',id:3},
{name:'bagel',id:4}
)
日志({list});
let filtered=list.filter(e=>e.id!==search\u id)
日志({filtered});
如果(过滤的长度)
日志(`Found${filtered.length}匹配!`);
其他的
日志(`未找到${search\u id}的id`);
//简单的记录器,因此标签在其自己的行上
函数日志(){
设[param1,…args]=参数;
开关(参数类型1){
大小写“string”:
if(参数长度)
log(`${param1}:`),//逗号
console.log(args.pop())
其他的
console.log(param1);
打破
案例“对象”:
for(输入参数1)
log(`${key}:`),//逗号
console.log(param1[key])
打破
违约:
console.log('传递的参数出错。无法识别类型')
}      

}
你不能像那样比较数组;数组是对象,JavaScript比较对象仅按标识比较对象。使用
.find()
.findIndex()
而不是
.indexOf()
list=list.filter(e=>e.id!==2)
可能的重复纯粹是一种观察意见(不是侮辱或屈尊),我发现有趣的是,你认识到这种行为与你的意图完全相反,而且你没有想到切换操作员来反映你的意图。我真傻,竟然没想到这就是我要找的东西!