1

I have two arrays and each one contain 14 items and a main for loop (I want i in this loop to do some behaviors).

I want to assign pass variable along with iterating i in the for loop with items of values array.

Here is what I mean:

var values = [1, 3, 1, 1, 1, 1, 2, 1, 2, 1, 2, 2, 1, 3];
var indexs = [3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
var pass = 0;
for (var i = 0; i < 1000; i++) { 
    // when i is equel to each item in "indexs" array assign "pass" with "values" item
    // for example if i == 3  ===>  pass = values[0] or 1
    // if i == 5  ===>  pass = values[1] or 3
    // if i == 18  ===>  pass = values[14] or 3
}
3
  • 1
    for (var i = 0; i < 14; i++) { // i will never = 18 as 18 is > 14. I don't understand what you're trying to do in this question, what does it mean that pass should have either of two values? It can only have one value. Commented Jun 27, 2019 at 18:51
  • 2
    You need to clarify better what you are trying to do. Commented Jun 27, 2019 at 18:52
  • pass = values[indexs.indexOf(i)] will get the first match. Commented Jun 27, 2019 at 18:52

1 Answer 1

1

If I understood correctly you want to check whether your indexs array contains index i of for variable. If so, try to use includes function:

const values = [1, 3, 1, 1, 1, 1, 2, 1, 2, 1, 2, 2, 1, 3];
const indexs = [3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
let pass = 0;
for (let i = 0; i < 14; i++) { 
    if (indexs.includes(i)) {
      let arrayIndex = indexs.indexOf(i);
      pass = indexs[arrayIndex];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for understanding me with my poor English:) Thats it: pass = values [arrayIndex];
@SaraRee I am glad to help you!:)

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.