JavaScript-如何从包含特定字符串的数组中删除对象? [英] JavaScript - How do I remove objects from an array that contain a specific string?
本文介绍了JavaScript-如何从包含特定字符串的数组中删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数组有几个对象.一些对象包含特定的字符串,如果对象包含此特定的字符串,我希望将其从数组中删除.
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.
我尝试使用 while 循环查找数组中所有具有臭味"的索引.然后拼接每个.
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.
最后,我只希望数组包含
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)
推荐答案
将过滤器用作:
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)
这篇关于JavaScript-如何从包含特定字符串的数组中删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文