0

I have an object that contains multiple ids in an array:

"Headers" [
  {
    ...
    "listOfIds": [236, 242, 250, 289],
    ...
  }
]

In my react component how can I use this array to compare if the ids in my redux store matches with any of the ones in the array. If it contains any of those ids, then I need to apply the class name to the variable.

My current code:

const reduxState = useSelector(state=> state);

_.map(Headers, (item, index) => {
  const { listOfIds } = item;
  let className = '';
  if(_.get(reduxState, 'common.id', null) != listOfIds) {
    className = 'hidden'
  }
})
2
  • Your question title and question body differ considerably. All you appear to be trying to do is searching an array to see if a property's value matches any of its members... Or to put it another way, if the array includes your property value. Commented Sep 27, 2021 at 19:14
  • It is, of course, unclear what Headers is, or why you're using _.map without using the resulting array, or what you're going to do with className, since it loses scope once the map's iteration has completed... Commented Sep 27, 2021 at 19:16

2 Answers 2

2

i dont use react but maybe you could use the "includes()" function of array types.

For example:

let array = [236, 242, 250, 289];
let find = 250;
if (array.includes(find)) { 
// your code
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works!
0

You can use Array.some:

const hasCommonId = Headers.some(({listOfIds}) => listOfIds.includes(reduxState?.common.id));

let className;
if (hasCommonId) {
  className = 'hidden'; // this should probably be a call to setState
}

Comments

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.