0

I have a following array: [400, 600, 1000] - I need to see a changing/dynamic value against this array if it equals to any of the values inside the array and return the index of the value matched.

Tried:

arr.every(x => x = dynamicValue );  

This return boolean

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
monkeyjs
  • 604
  • 2
  • 7
  • 29

1 Answers1

1

If you want to find index:

let yourArray = [400, 600, 1000];
let yourVariable = 600;
let indexElement = yourArray.indexOf(yourVariable); 

UPDATE:

To get indexes of wish elements of array:

let yourArray = [400, 600, 1000];    
let yourVariable = 1000;
let yourIndexes = [];
yourArray.forEach((e, i)=>{
        if(e < yourVariable) 
            yourIndexes.push(i);
    });
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • @https://stackoverflow.com/users/1646240/stepup Is there a way I get all indices that are less than 600 here? filter returns updated array with matched values - but how to get the indices - here I need to get [0,1] - when checked value in array – monkeyjs Dec 07 '18 at 21:31
  • @monkeyjs please, see my updated answer. – StepUp Dec 07 '18 at 21:47
  • works like charm. Thank you so much @StepUp. – monkeyjs Dec 07 '18 at 22:27
  • @monkeyjs I've found a bag in my implementation. Now it works properly. See necessary indexes in `yourIndexes` array. Feel free to ask any question. If you found that my reply is helpful, you can mark it as an answer to simplify the future search of other people. – StepUp Dec 08 '18 at 11:42