0

I have an array with 9 elements wich are HTML elements as you can see in the picture below.

Object array

I'am trying to create a function wich checks if there is any text inside each of those 9 elements

I have tried to run Array.ForEach method to check if there is an "X" inside the text , but I can't do it , maybe because Iam not sure how to acess the innerText of the propertie :

Board.SquaresArray.ForEach(function(){

if (Board.SquaresArray.InnerText =="X") return console.log("This square has an X")};

5
  • 1
    Welcome to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? Please post code, data structures, error messages, markup, etc. as text, not as a picture of text. Why: meta.stackoverflow.com/q/285551/157247 Commented Oct 12, 2020 at 14:40
  • Would filter not wok here? Commented Oct 12, 2020 at 14:40
  • First of all, ForEach isn't spelled that way, it is camel-case: forEach. same with "InnerText" Commented Oct 12, 2020 at 14:40
  • See the linked question's answers for details, but the short version is: if (theArray.some(e => e.innerText)) { Commented Oct 12, 2020 at 14:45
  • Assuming the object properties is what your are trying to filter for empty text try Object.entries + filter like recommended above Commented Oct 12, 2020 at 14:46

1 Answer 1

1

If Board.SquaresArray is a result of document.querySellector() or something like this, you need to convert it from HTMLCollection to Array:

Array.from(Board.SquaresArray).some(el => el.innerText === 'X')

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

1 Comment

Board.SquaresArray is a normal array not a collection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.