过滤对象数组

时间:2018-08-09 13:31:31

标签: javascript node.js

我正在尝试检查Array中的对象之一的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');
}

6 个答案:

答案 0 :(得分:1)

然后仅使用!==代替===

但是您可以使用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');

答案 1 :(得分:0)

您正在使用过滤器,就像过滤掉元素一样,而实际上保留了适合条件的元素。将条件更改为e.id !== 2,它会保留所有 ids不等于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));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

您需要颠倒filter谓词函数的逻辑。

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

filter仅返回与谓词匹配的项目-在您的情况下,它将仅返回ID为2的一个元素的列表。

答案 3 :(得分:0)

使用<html> <head> <title>Row Click</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> function test(){ alert('test'); } $(document).ready(function(){ var row='<tr onclick="test()"><td >Value 4</td><td>Value 5</td><td>Value 6</td></tr>'; $("#myTable").append(row); }); </script> </head> <table id="myTable" > <th>Column 1</th><th>Column 2</th><th>Column 3</th> <tr onclick="test()"> <td >Value 1</td> <td>Value 2</td> <td>Value 3</td> </tr> </table> </html> 测试时,您正在搜索具有找到的元素的数组。

os.system('wpscan -u'+wpsite+'--enumerate u') 返回带有结果的数组。您应该使用indexOf来返回单个元素:

filter

答案 4 :(得分:0)

使用list.filter(e => e.name === 2)时。它将返回一个 array 包含对象,而不是对象本身。因此它将返回-1。您可以使用传播语法从包含它的数组中提取对象:

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

答案 5 :(得分:0)

Installing laravel/laravel (v5.6.21) - Installing laravel/laravel (v5.6.21): Loading from cache Created project in laravel @php -r "file_exists('.env') || copy('.env.example', '.env');" Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 70 installs, 0 updates, 0 removals - Installing vlucas/phpdotenv (v2.5.0): Downloading (100%) [ErrorException] copy(/Users/mohammadreza/.composer/cache/files/vlucas/phpdotenv/860e02c1487d4eaac63d29a77c5fa31c2739df62.zip): failed to open stream: Permission denied 将不起作用,因为您将搜索对象,并且JavaScript中的对象比较非常棘手(需要使用自定义评估程序来检查相同性)。好消息是您可能不需要它。

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

在下面的代码中,我包括了一些其他可能会有价值的东西,例如:

  • !==
  • 速记属性名称(例如Array.of()
  • 使用逗号运算符(即{ foo } === {'foo': foo})执行系列,尤其是避免大括号
  • ,作为函数参数的rest运算符
  • 和其他人

...

相关问题