skip to Main Content

1 – I am using the below code to verify if every element of one array is present in another array.

     let expected_ActionLinks = ["Add to Call List", "ChangeOwner","this","Edit","Delete","Sharing","MI/User Reg",
     "Convert","Disqualify","Email Preferences"]

     let actual_ActionLinks = ["Add to Call List", "ChangeOwner","Edit","Delete","Sharing","MI/User Reg",
     "Convert","Disqualify","Email Preferences"]



   try{ 

   
    
    if(expected_ActionLinks.some(async (element)=>{ actual_ActionLinks.indexOf(element) > -1 }))
    {

        console.log("All action links are present on the page")
        return true
    }

    else{

        console.log("All action links are present on the page")
        return false 

    }

}
    

I am trying to verify if every element of one array is present in the other array.
For example here – every element of expected_ActionLinks should be present in actual_ActionLinks. If yes, it will return true, if no it will return false.

The problem here is the above code returns always true irrespective of any situation. Please suggest

2

Answers


  1. Remove async add return

    function check() {
      let expected_ActionLinks = ["Add to Call List", "ChangeOwner", "this", "Edit", "Delete", "Sharing", "MI/User Reg",
        "Convert", "Disqualify", "Email Preferences"
      ]
    
      let actual_ActionLinks = ["Add to Call List", "ChangeOwner", "Edit", "Delete", "Sharing", "MI/User Reg",
        "Convert", "Disqualify", "Email Preferences"
      ]
    
      if (expected_ActionLinks.some((element) => {
          return actual_ActionLinks.indexOf(element) > -1
        })) {
    
        console.log("All action links are present on the page")
        return true
      } else {
    
        console.log("All action links are present on the page")
        return false
    
      }
    }
    
    check()
    Login or Signup to reply.
  2. You can use .every of Array

    let expected_ActionLinks = ["Add to Call List", "ChangeOwner", "this", "Edit", "Delete", "Sharing", "MI/User Reg",
      "Convert", "Disqualify", "Email Preferences"
    ];
    
    let actual_ActionLinks = ["Add to Call List", "ChangeOwner", "Edit", "Delete", "Sharing", "MI/User Reg",
      "Convert", "Disqualify", "Email Preferences"
    ];
    
    const isSame = expected_ActionLinks.every(item => actual_ActionLinks.includes(item))
    
    console.log(isSame)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search