0

I have the following code:

<button className={`actionBoxButton ${props.moves[0].moveName !== "FirstPassMove" && props.moves[0].moveName !== "PassMove"  ? "actionBoxButtonGrey" : ''}`}

props.moves looks like this:

{userId: 30, moveId: 60, moveName: "FirstSettlementMove", building: {…}}
1: {userId: 30, moveId: 62, moveName: "FirstSettlementMove", building: {…}}
2: {userId: 30, moveId: 64, moveName: "FirstSettlementMove", building: {…}}
3: {userId: 30, moveId: 66, moveName: "FirstSettlementMove", building: {…}}
4: {userId: 30, moveId: 68, moveName: "FirstSettlementMove", building: {…}}
5: {userId: 30, moveId: 70, moveName: "FirstSettlementMove", building: {…}}
6: {userId: 30, moveId: 72, moveName: "FirstSettlementMove", building: {…}}
7: {userId: 30, moveId: 74, moveName: "FirstSettlementMove", building: {…}}

This checks if in the props.move (which is an object not an array) element 0 has a key moveName !== FirstPassMove or PassMove. But I do not want to check for element 0 only but all elements of the object. Some said I should use Array.some but since it is not an array that did not work.

Greetings

1
  • Please show how props.move looks like beforehand. Commented May 4, 2020 at 15:38

2 Answers 2

1

What you needs in your case is Object.values from that object and iterate over it.

Example:

const moves = {
  0: {
    moveName: "FirstPassMove"
  },
  1: {
    moveName: "PassMove"
  },  
  2: {
    moveName: "Other"
  },
}

const isTheRightMove = Object.values(moves).some(move => ["FirstPassMove", "PassMove"].includes(move.moveName))

console.log(isTheRightMove) // true

const moves2 = {
  0: {
    moveName: "Other"
  },
  1: {
    moveName: "Other"
  },  
  2: {
    moveName: "Other"
  },
}



const isTheRightMove2 = Object.values(moves2).some(move => ["FirstPassMove", "PassMove"].includes(move.moveName))

console.log(isTheRightMove2) // false

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

Comments

0

you can do in this way

<button className={`actionBoxButton ${props.moves.find((move) => move.moveName !== "FirstPassMove" && move.moveName !== "PassMove") != null  ? "actionBoxButtonGrey" : ''}`}

1 Comment

this gives me an error that find(move) is not a function

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.