[英]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.