Home > Back-end >  Filter JSON objects based on array variable with multiple attributes in javascript
Filter JSON objects based on array variable with multiple attributes in javascript

Time:11-25

Context I am using: Windows 11, VSCode and node v18.12.1

I am trying to filter a JSON object, using an array variable, as I have more than one value I want to filter against.

Question I have the following code working which filters based on a single user's name. What I want to do is filter based on matching one or more of the names.

Here is the code that's working filtering on one owner's name

let carOwners = [{ "name": "Phil", "age": 52, "car": null },
{ "name": "Carl Phil", "age": 25, "car": "Tesla" },
{ "name": "Bob", "age": 65, "car": "Land Rover" },
{ "name": "Megan", "age": 34, "car": "Mercedez" },
{ "name": "Charlene", "age": 67, "car": "Mazda" }]

checkUsers = carOwners.filter(user => !(user.name.includes("Phil")));

console.log(checkUsers);

which gives the result:

[
  { name: 'Bob', age: 65, car: 'Land Rover' },
  { name: 'Megan', age: 34, car: 'Mercedez' },
  { name: 'Charlene', age: 67, car: 'Mazda' }
]

What I want to do is filter out any results that contain Phil or Bob.

What I've tried I have searched various other solutions, but they don't seem to cater for what I'm after...

This is the first method tried:

filteredNames = ['Phil', 'Bob'];

checkUsers = carOwners.filter(user => !(user.name.includes(filteredNames)));

console.log(checkUsers);

The result I'm after is:

[
  { name: 'Megan', age: 34, car: 'Mercedez' },
  { name: 'Charlene', age: 67, car: 'Mazda' }
]

except I get:

[
  { name: 'Phil', age: 52, car: null },
  { name: 'Carl Phil', age: 25, car: 'Tesla' },
  { name: 'Bob', age: 65, car: 'Land Rover' },
  { name: 'Megan', age: 34, car: 'Mercedez' },
  { name: 'Charlene', age: 67, car: 'Mazda' }
]

I tried numerous others, the closest I got was based on the solution here I tried the following:

const filteredNames = ['Phil', 'Bob'];

let checkUsers = carOwners.filter(a => !(filteredNames.includes(a.name   '')));

console.log(checkUsers);

which is closer (as it's filtered out objects with the names Bob and Phil), however I am still returned the object 'Carl Phil', as you can see below... The question I specifically have is how do I filter out any object that contains any appearance of any of the names in the filteredNames array, so that I am left with the objects with the name 'Megan' and 'Charlene'?

[
  { name: 'Carl Phil', age: 25, car: 'Tesla' },
  { name: 'Megan', age: 34, car: 'Mercedez' },
  { name: 'Charlene', age: 67, car: 'Mazda' }
]

Any advice appreciated. Thanks in advance

CodePudding user response:

Since you have to filter an occurrence of the given names anywhere in the string, a good catch is to use regular expressions. In this case, we're looking for names that do not match some of the filteredNames.

You can use a combination of Array.prototype.some() and String.prototype.match() to do so.

let carOwners = [{ "name": "Phil", "age": 52, "car": null },
{ "name": "Carl Phil", "age": 25, "car": "Tesla" },
{ "name": "Bob", "age": 65, "car": "Land Rover" },
{ "name": "Megan", "age": 34, "car": "Mercedez" },
{ "name": "Charlene", "age": 67, "car": "Mazda" }]

let filteredNames = ['Phil','Bob']

let newObj = carOwners.filter(({name}) =>
    !filteredNames.some(filtered => name.match(filtered)))

console.log(newObj)

CodePudding user response:

The smarter solution is to use regular expression as testing_22 posted.
The following codes are not cool but more understandable for people who don't like regular expression :)

const carOwners = [
  { "name": "Phil", "age": 52, "car": null },
  { "name": "Carl Phil", "age": 25, "car": "Tesla" },
  { "name": "Bob", "age": 65, "car": "Land Rover" },
  { "name": "Megan", "age": 34, "car": "Mercedez" },
  { "name": "Charlene", "age": 67, "car": "Mazda" }
];

const filteredNames = ['Phil', 'Bob'];

const checkUsers = carOwners.filter(({name}) => {
  for (const [index, eachName] of filteredNames.entries()) {
    if (name.includes(eachName)) {
      return false;
    }
  }
  return true;
});

console.log(checkUsers);
  • Related