Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check the array within array if the value is the same on the newArray

How I can scan my array within array if there is equal array element.
I want to check if its true or false

// the array to be scan 
const array = [
[0, 1, 2],
[3, 4, 5], 
[6, 7, 8],
]

// the new array
const newArray = [0, 1, 2]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Based on anwser from how-to-compare-arrays-in-javascript,you just need to iterate the first array and then compare them

const array = [
[0, 1, 2],
[3, 4, 5], 
[6, 7, 8],
]

// the new array
const newArray1 = [0, 1, 2]
const newArray2 = [0, 1, 3]

const checkArray = (array1,array2) => {
  for(let arr of array1){
   if(arr.length === array2.length && arr.every((v,i) => v === array2[i])){
     return true
   } 
  }
  return false
}

console.log(checkArray(array,newArray1))
console.log(checkArray(array,newArray2))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading