Filter array of object


#1

Hello!

I have an array of object. Something like:

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"},
];

I need to filter the array , removing the field that contains a word.
For example , I need to filter the element that contains the whole word:"Lets".

So the array should now be:

var data = [   
     {id: "John is better than me", string: "I'm ok"},
     {id: "last but not least", string: "for sure"},
];

I tried with for/loop and slice , but it needs the whole string (i.e." Lets go home ")
Anyone could suggest me the right way?


#2

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);

#3

Thank you Paul, that's all I needed.
I'm a newbee, can I ask something more to learn?

  1. What does ! means? what is its function?

  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 ?

  3. What if "string" would be a number : {id: "John is better than me", string: 2} . I tried , but I get an error.

Thank you for your kindness.


#4

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.

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.

  return !Object.values(item).find(function (value) {
  	return value.includes("Lets");
  });

That problem happens with the above code.

We can use the map method to convert the array of values to strings instead, before checking them.

This is what t looks like in full:

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 !Object.values(item).map(function (value) {
    return String(value);
  }).find(function (value) {
  	return value.includes("Lets");
  });
});
console.log(data);

And can be seen working (turn on the your browser console), at https://jsfiddle.net/tygr3rrh/


#5

Putting some of the code in a function, can help to make it easier to understand what's going on there too.

function includesStr(values, str) {
  return values.map(function (value) {
    return String(value);
  }).find(function (value) {
    return value.includes("Lets");
  });
}

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"},
];
var filtered = data.filter(function (item) {
  return !includesStr(Object.values(item), "Lets");
});
console.log(filtered);

https://jsfiddle.net/tygr3rrh/2/


#6

Thank you Paul, really exaustive!! I learned a lot!


closed #7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.