Here’s a good way to do it using filter, and the includes method:
var data = [
{id: 1, string: "Lets go home"},
{id: "John is better than me", string: "I'm ok"},
{id: "last but not least", string: "for sure"},
];
data = data.filter(function (item) {
return !item.string.includes("Lets");
});
console.log(data);
Thank you Paul, that’s all I needed.
I’m a newbee, can I ask something more to learn?
What does ! means? what is its function?
I’m ok with your code, but what if I needed to match on every key ? I mean , what should it be if I’d need to check “lets” is in “id” , " string", or in others key/val ?
What if “string” would be a number : {id: “John is better than me”, string: 2} . I tried , but I get an error.
The ! symbol is the negation symbol. So it turns true to false, and false to true.
In this case we are checking if the string does not include what you’re checking for.
[quote=“diegosaggiorato, post:3, topic:286730, full:true”]
2. I’m ok with your code, but what if I needed to match on every key ? I mean , what should it be if I’d need to check “lets” is in “id” , " string", or in others key/val ?[/quote]
There are several object methods that you can use, such as keys, or entries, or values. I’d use values in this case.
Here’s how I’d get there. First I get the values and log them out, to make sure that I’m referencing the right thing:
data = data.filter(function (item) {
console.log(Object.values(item));
return !item.string.includes("Lets");
});
The Object.values method gives an array, so we can filter that, but in this case we can return if didn’t find anything with the array find method.