0

I am attempting to filter out array data and have my return be newData filtering out the index that includes the title "Tourism"

I am using the .filter method and using .includes but when doing so newData return value includes "Tourism" even though I putting a check to return all values that does not include this.

Here is a code example of what I am working with :

let data = [ { title: "Vacation", revenue: 23421 }, { title: "Hospitals", revenue: 34212 }, { title: "Tourism", revenue: 42124 }, {title: "International Tourism", revenue: 87321 } ]


newData = data.filter(function (item) {
  return !item.title.includes('Tourism') || item.title.includes('Vacation')
})

console.log(newData)

I am expecting the returning values to not include "Tourism" and "International Tourism" based off the .includes returning everything that does not include "Tourism". Am I approaching this scenario the correct way? My current value is returning everything within the array regardless of my .includes check

1 Answer 1

2

includes is a method, so it is a function which takes an argument, specifically the value to check for in the string or array it is called on. Use .includes(value) instead:

let data = [{
  title: "Vacation",
  revenue: 23421
}, {
  title: "Hospitals",
  revenue: 34212
}, {
  title: "Tourism",
  revenue: 42124
}, {
  title: "International Tourism",
  revenue: 87321
}];

newData = data.filter(
  item => !item.title.includes('Tourism')
);

console.log(newData);

For your edit, if you want to filter out multiple values, you can use a few ways to say the same thing. Consider !includesTitle() && !includesVacation() ("doesn't include title and doesn't include vacation") and !(includesTitle() || includesVacation) ("doesn't include title or location"), which are semantically equivalent:

let data = [ { title: "Vacation", revenue: 23421 }, { title: "Hospitals", revenue: 34212 }, { title: "Tourism", revenue: 42124 }, {title: "International Tourism", revenue: 87321 } ];

// filter for items where item doesn't include 'tourism' or 'vacation'
newData = data.filter(
  // using ! to exclude any that match:
  item => !(
    // Tourism, OR
    item.title.includes('Tourism') ||
    // Vacation
    item.title.includes('Vacation')
  )
);

console.log(newData);

Sign up to request clarification or add additional context in comments.

6 Comments

This is what I was expecting, thank you! will accept your answer once the time limit is over.
No worries. To elaborate a bit on what happened, you were basically checking if String.includes == 'Tourism', which would never be true (or false, in your case, since it was negated, so it always returned true). console.log(item.title.includes) would print something like function() { [native code] }, because you would be referring to the method String.prototype.include itself. Just for all the gory details.
I appreciate the details. In the event of checking multiple conditions, for example eliminating 'Tourism' and` 'Vacation' adding a || to the code you provided returns back the original data array. Am I approaching that next step the wrong way?
@maimok I actually messed up in one of my edits, I had it filtering for items with "Tourism" in the title. Adding ! inverted it. If you edit your question and tag me, I will edit my answer and expand on the use case. If you wanted to exclude anything that contained "Tourism" or "International", you'd do item => !(item.title.includes('Tourism') || item.title.includes('Vacation')).
I appreciate your help, I had edited the code snippet above to include an "Tourism" or "Vacation" when expanding on the text you had provided it does not filter out the second part Vacation when adding an !item.title.includes('Vacation') it returns the original array with both Tourism and Vacation. This is where I am currently stuck at
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.