0

I have this array:

var test.qs =
[
 {"answer":null,
  "testQuestionId":710
  "synchronized":true},
 {"answer":null,
  "testQuestionId":711
  "synchronized":false
]

I would like to call a function on all the array objects that have the syncronized property equal to false like this:

if (!test.qs[x].synchronized) {
    httpPutTestQuestion(number)
    .success(function (data) {
        test.qs[x].synchronized = true;
    })
}

But how can I do this as if I put this into a for loop then when the function returns I won't still have the value x?

2
  • You missed the closing brace for second array record, just FYI :) Commented Jul 20, 2014 at 11:56
  • ... and a couple of commas. Also var test.qs is not valid javascript. Commented Jul 20, 2014 at 12:14

2 Answers 2

1

I think this should work for you.

test.qs.forEach(function (q) {
if (!q.synchronized) {
    httpPutTestQuestion(number)
    .success(function (data) {
        q.synchronized = true;
    })
}
})
Sign up to request clarification or add additional context in comments.

Comments

1

Why don't you use the plain old loop, like this

var i;
for (i = 0; i < qs.length; i += 1) {
    if (qs[i].synchronized === false) {
        httpPutTestQuestion(number)
            .success(function (data) {
                test.qs[x].synchronized = true;
            })
        };
    }
}

You can get the length of the array with the length property. Also note that, the variable names inJavaScript cannot have . in them. So,

var test.qs = ....

will fail.

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.