简体   繁体   English

Javascript Arrays 与包含相反

[英]Javascript Arrays Opposite of Includes

What is the correct way of removing objects that have a certain field in JavaScript?删除 JavaScript 中具有特定字段的对象的正确方法是什么?

I can include fields with the following:我可以包含以下字段:

filteredResult = filteredResult.filter(e => e.selectedFields.includes("Red"));

But if I wanted to remove all properties that have this field what would I do?但是,如果我想删除所有具有此字段的属性,我该怎么办? There doesn't seem to be a "Remove" from the documentation.文档中似乎没有“删除”。

只是否定它。

filteredResult = filteredResult.filter(e => !e.selectedFields.includes("Red"))

过滤结果 = 过滤结果.filter((l) => (['红色', '蓝色','黄色'].every(y => !l.selectedFields.toLowerCase().includes(y.toLowerCase()))) );

You can negate the includes result or probably cleaner by using the indexOf property:您可以使用indexOf属性否定includes结果或可能更清洁:

const arr = [1, 2, 3];

const notIncludes1 = (arr, val) => !(arr.includes(0));
const notIncludes2 = (arr, val) => arr.indexOf(val) === -1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM