0

I'm using Ionic 4 and i'd like to know how could I check if an array is NOT on other array to make a condition, but it doesn't work, what i'm doing wrong?

  method() {
    if (!this.array.includes(['Web development', 'Football'])
      ) {
        console.log(this.array)
        //doSomething
      }

UPDATED

once click on a button, imDone method starts. What i want exactly is once clicked, if the iDislike array (all this words) arent on the arrayChip, then do something

  ngOnInit() {    
this.arrayChip = [
  '4',
  "4",
  '5',
  '5',
  '7',
  '34',
  '1',
  '9',
  '7',
  '9',
  '4',
  '3',]
  }

  imDone() {
    let iDislike = ['Web 3', '2', '4', '5', '6']
    const includesListOfItemsToCheck = this.arrayChip.some(subArray => 
      iDislike.every(item => subArray.includes(item)));    
    console.log(includesListOfItemsToCheck)

    console.log('arrayChip: ' + this.arrayChip)
  }

1 Answer 1

2

Array.includes only works with primitive values like strings & numbers.. And you can't compare two arrays using === in JavaScript

so something like this should do the job -

const array = [['Soap'], ['Web development', 'Football'], ['Globe', 'Science', 'Spartan']]
const listOfItemsToCheck = ['Web development', 'Football']
const includesListOfItemsToCheck = array.some(subArray => subArray.every(item => listOfItemsToCheck.includes(item)));
console.log(includesListOfItemsToCheck);

Here I'm using Array.some which will return a boolean value if any item in the array satisfies the condition in my return statement. And Array.every which will return a boolean value determining whether every item in the array satisfies the condition in the return statement.

Update to answer the updated question:

  const arrayChip = ['4',"4",'5','5','7','34','1','9','7','9','4','3']
  const iDislike = ['Web 3', '2', '4', '5', '6']
  const allIDislikeDoesntExistInArrayChip = iDislike.every(item => !arrayChip.includes(item))
console.log(allIDislikeDoesntExistInArrayChip)
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks buddy, theres no other way easier? it seems too much complicated to me to understand this logic
By the way... it gives an error "subArray.every is not a function" why?
I'm sorry maybe i didnt say it correctly i just want to check if 3 strings are into an array.... that's all
if you want to check if 3 strings are in an array as opposed to checking that the subArray is equal to the listOfItemsToCheck you can do const includesListOfItemsToCheck = array.some(subArray => listOfItemsToCheck.every(item => subArray.includes(item))); instead. This is really easy and readable once you understand Array.some and Array.every, the Alternative would be doing nested forEach loops, which would uglier, more verbose and less readable IMO.
@M.Mariscal running the answer on the browser works fine and returns true as expected, can you show me your updated code? Also checkout developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
|

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.