2

How to parse an object like this with jQuery or javascript:

Object { 4: Array[1], 5: Array[1] } //arrSrok

$.each(arrSrok, function(srokID, arrKhum) {
  console.log(arrKhum); //runs only once, and I got srokID = 4, arrKhum empty
});

It seems that arrKhum cannot be assigned with an array. What do I miss?

Though I can parse an object with the following format successfully

Object { 1: Object, 2: Object }

1 Answer 1

3

This is what I tried and it seems to be working correctly

var someObject = { 4: [3,4], 5: [1,2] };

EDIT: If you want to loop through array, you can use $.isArray provided by JQuery

$.each(someObject, function(key, value) {
  if($.isArray(value)){
     $.each(value,function(key1,value1){
      console.log(value1);
     });
  }
});

Here is the Link

JS Fiddle

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

3 Comments

I did not test it with $.isArray, but it should not be a problem if we already know that it is an array. It seems that I followed the same syntax. I will also check if the problem is somewhere else. Thanks anyway.
Yeah..it shudnt be a problem if you know its already an array
My problem just disappeared miraculously, and the parsing works perfectly now using similar way to your suggested one (without $.isArray() testing). I did not find the cause of the problem because I just restarted localhost server and it worked. Anyway, thanks a lot, and I accept your answer.

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.