1

How may I get the following but without manually writing each key? I want the if statement to include every colors key.

if (colors[0].val.join() === colors[colors[0].next].orig.join() &&
    colors[1].val.join() === colors[colors[1].next].orig.join() &&
    colors[2].val.join() === colors[colors[2].next].orig.join() &&
    colors[3].val.join() === colors[colors[3].next].orig.join() &&
    colors[4].val.join() === colors[colors[4].next].orig.join())
{ }

All I can come up with is:

colors.forEach(function(k, i) {
    colors[i].val.join() === colors[colors[i].next].orig.join();
});
0

1 Answer 1

3

Use the .every() method

if (colors.every(function(el) { return el.val.join() === colors[el.next].orig.join(); })) {
    // do something
}

It will work on every browser, besides IE < 9.

If you need older IE support, polyfill the method (a polyfill is available on the MDN documentation)

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

1 Comment

Ah, I see; I hadn't seen the property listed in the original question (or I'd glossed over it somehow).

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.