简体   繁体   English

JavaScript - 如何从包含特定字符串的数组中删除对象?

[英]JavaScript - How do I remove objects from an array that contain a specific string?

My array has several objects.我的数组有几个对象。 Some of the objects contain a specific string, if the object contains this specific string I want it to be removed from the array.某些对象包含特定字符串,如果对象包含此特定字符串,我希望将其从数组中删除。

I tried to use a while to loop and find all of the indexes of the array that have "Smelly" and then splice each one.我尝试使用while循环并找到具有“臭味”的数组的所有索引,然后将每个索引拼接起来。

In the end I want the array to only contain最后我希望数组只包含

[  
    name: "Tiffany", status: "Clean",
    name: "Space-man", status: "Clean",
    name: "Soap-man", status: "Clean",
]

 let myArray = [ {name: "Timmy", status: "Smelly"}, {name: "Tiffany", status: "Clean"}, {name: "Kyle", status: "Smelly"}, {name: "Space-man", status: "Clean"}, {name: "Soap-man", status: "Clean"}, ] while (i = 0) { try { let find = myArray.findIndex(i => i.status === `Smelly`); myArray.splice(find, 1); } catch { i = 1; } } console.log(myArray)

You can get the result using Array.filter您可以使用Array.filter获得结果

 const value = [ {name: "Timmy", status: "Smelly"}, {name: "Tiffany", status: "Clean"}, {name: "Kyle", status: "Smelly"}, {name: "Space-man", status: "Clean"}, {name: "Soap-man", status: "Clean"} ]; const result = value.filter((item) => item.status !== 'Smelly'); console.log(result);

Just filter it过滤一下就行

You can choose to create a new filtered array or replace the existing one您可以选择创建一个新的过滤数组或替换现有的数组

 let myArray = [ {name: "Timmy", status: "Smelly"}, {name: "Tiffany", status: "Clean"}, {name: "Kyle", status: "Smelly"}, {name: "Space-man", status: "Clean"}, {name: "Soap-man", status: "Clean"}] // filter into a new array const cleanArray = myArray.filter(item => item.status !== "Smelly") console.log(cleanArray) // or replace: myArray = myArray.filter(item => item.status !== "Smelly") console.log(myArray)

Use filter as:使用过滤器作为:

 let myArray = [ {name: "Timmy", status: "Smelly"}, {name: "Tiffany", status: "Clean"}, {name: "Kyle", status: "Smelly"}, {name: "Space-man", status: "Clean"}, {name: "Soap-man", status: "Clean"}, ] let result = myArray.filter(i => i.status !== 'Smelly'); console.log(result)

If you like to kee the object reference of the array, you could iterate from the end of the array.如果你想保持数组的对象引用,你可以从数组的末尾迭代。

Array#slice changes the indices if spliced from start.如果从头开始拼接, Array#slice更改索引。

 let array = [{ name: "Timmy", status: "Smelly" }, { name: "Tiffany", status: "Clean" }, { name: "Kyle", status: "Smelly" }, { name: "Space-man", status: "Clean" }, { name: "Soap-man", status: "Clean" }], i = array.length; while (i--) if (array[i].status === `Smelly`) array.splice(i, 1); console.log(array);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

First of all you need to have a boolean in the while loop.首先,您需要在 while 循环中有一个布尔值。 So you can add a counter starting from 0 and run the loop while the counter is less than the length of the array .因此,您可以添加一个从 0 开始的计数器,并在计数器小于数组长度时运行循环。

Here is the code:这是代码:

 let myArray = [ { name: "Timmy", status: "Smelly" }, { name: "Tiffany", status: "Clean" }, { name: "Kyle", status: "Smelly" }, { name: "Space-man", status: "Clean" }, { name: "Soap-man", status: "Clean" }, ] let counter = 0; while (counter < myArray.length) { let find = myArray.findIndex(i => i.status === `Smelly`); if (find != -1) { myArray.splice(find, 1); } else { counter++; } } myArray.forEach(el => console.log(el));

splice is expensive. splice很贵。 It has to shift all subsequent elements after the one you've removed.它必须在您删除的元素之后移动所有后续元素。 And you do that in a loop.你在一个循环中这样做。

Most of the time I'd go with Array#filter .大多数时候我会使用Array#filter It's simple and it's safe;它简单且安全; no unexpected side effects when I "remove" items from a list (like something disappearing because I forgot, that some other code also uses this list) .当我从列表中“删除”项目时没有意外的副作用(比如因为我忘记了一些其他代码也使用了这个列表而消失的东西)

But sometimes I explicitely want or need to mutate the list itself.但有时我明确地想要或需要改变列表本身。 Here's a little utility function for that:这是一个小实用函数:

Like splice it shifts all elements towards the low indices, and like filter it keeps only the elements that match the predicate.像 splice 一样,它将所有元素移向低索引,像 filter 一样,它只保留与谓词匹配的元素。

 const value = [ {name: "Timmy", status: "Smelly"}, {name: "Tiffany", status: "Clean"}, {name: "Kyle", status: "Smelly"}, {name: "Space-man", status: "Clean"}, {name: "Soap-man", status: "Clean"} ]; function filterInPlace(array, predicate) { let j = 0; for (let i = 0; i < array.length; ++i) { if (i in array && predicate(array[i], i, array)) { if (i !== j) { array[j] = array[i]; } ++j; } } array.length = j; return array; } const result = filterInPlace(value, (item) => item.status !== 'Smelly'); console.log(result === value); console.log(result);

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

相关问题 如何从仅包含字符串的数组中删除项目? - How do I remove items from an array that only contain a string? 在javascript中,如何从对象数组中删除元素? - In javascript, how do I remove an element from an array of objects? 如何从 JavaScript 关联数组中删除对象? - How do I remove objects from a JavaScript associative array? 如何从Javascript字符串中删除特定的“序列”? - How do I remove a specific “sequence” from a Javascript string? JavaScript:如何删除包含特定字符串的行 - JavaScript: how to remove line that contain specific string 对象数组如何检查深度嵌套的文本字符串重复项并从数组中删除? - Array of objects how do i check for deeply nested text string duplicates & remove from array? 如何递归删除包含空数组的嵌套对象? - How do you recursively remove nested objects that contain an empty array? 如何在 Javascript 中从对象数组中排除包含特定键和值对的项目 - How in Javascript to esxclude from an Array of Objects the items which contain specific pair of keys and values 如何在Javascript对象数组中搜索特定值? - How do I search for a specific value in an array of objects in Javascript? Javascript:如何在我的阵列中使用特定键删除 object? - Javascript: How do I remove an object with a specific key in my array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM