Karim Abou El Naga
Karim Abou El Naga

Reputation: 121

Reading values of an Javascript object with various keys

I am looking for a solution for the following problem. I am working with an object that looks:

0: {userId: 139, moveId: 169, moveName: "FirstRoadMove", building: {…}}
1: {userId: 139, moveId: 171, moveName: "FirstRoadMove", building: {…}}
2: {userId: 139, moveId: 173, moveName: "FirstRoadMove", building: {…}}
3: {userId: 139, moveId: 175, moveName: "FirstRoadMove", building: {…}}
4: {userId: 139, moveId: 177, moveName: "FirstRoadMove", building: {…}}
5: {userId: 139, moveId: 179, moveName: "FirstRoadMove", building: {…}}

And I also have a component:

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

What I am trying to achieve is to not only check for the button if the first elements of moves has a key with value FirstSettlementMove e.g. but every element of the object. So in semi pseudo something like:

    <button className={`actionBoxButton ${props.moves[first to last].moveName !== "FirstSettlementMove"  ? "actionBoxButtonGrey" : ''}`}

Thank you in advance!

Upvotes: 0

Views: 38

Answers (1)

Quentin Grisel
Quentin Grisel

Reputation: 4987

You could change your way of thinking. Instead of trying to check all sub object with moveName = firstSettleMentMove, you could check if there is a sub object without this value by doing as follow:

props.move.filter(x => x.moveName !== 'FirstSettlementMove').length > 0

If props.move isn't an array, you can still use Array.from(props.move) to convert it.

Also, if your object has indexes as you showed us, you can 'remove' it with Object.values(props.move).

Upvotes: 2

Related Questions