0

loop for array with sub arrays is not showing. Kindly advice. i am able to show the first array set but not able to show sub arrays of each set.

var json = [{
"Friends":[
    {
        'image'     : '_assets/images/users/01.jpg',
        'unread'    : '22',
        'name'      : 'Salman Razak',
        'message'   : 'way to be happy...',
        'lastchat'  : '16th Feb 2015 | 9:30 pm'
    },
    {
        'image'     : '_assets/images/users/02.jpg',
        'unread'    : '22',
        'name'      : 'Shahid Saeed',
        'message'   : 'way to be happy...',
        'lastchat'  : '16th Feb 2005 | 9:30 pm'
    }
],
"Colleagues":[
    {
        'image'     : '_assets/images/users/02.jpg',
        'unread'    : '22',
        'name'      : 'Hyder Memon',
        'message'   : 'way to be happy...',
        'lastchat'  : '16th Feb 2015 | 9:30 pm'
    }
]
}];

$.each(json, function () {
   $.each(this, function (name, value) {
      console.log(name + '=' + value);
      $('ul').append('<li>'+ name + ', ' + json[name].join() +'</li>');
   });
});
5
  • 2
    What is expected output ? Commented Feb 11, 2016 at 4:16
  • loop of back end data of contacts, contacts are divided into groups, such as friends, colleagues, etc. depending upon data filled in array i want to categories contacts. Commented Feb 11, 2016 at 4:18
  • 1
    Why is json an array? Commented Feb 11, 2016 at 4:19
  • What you have there is an array with a single object that has a Friends and a Colleagues property which themselves are arrays. You seem to think you have an array of arrays however. Commented Feb 11, 2016 at 4:19
  • You'll have to nest another iteration. Right now, you are iterating the objects hold by an array. You'll need to iterate the arrays hold by the properties of the object hold by an array. Commented Feb 11, 2016 at 4:22

2 Answers 2

1

Here is a loop that works with the data structure you have (working jsbin)

json.forEach(function(item){
    for(var group in item){
        if(item.hasOwnProperty(group)){
            var groupItems = item[group];
            groupItems.forEach(function(person){
                console.log(group, person.name);
            });
        }
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

loop of friends repeating 2 times, can i show it as 1 with almost 20 sub items
not sure I understand your question, this loop is specifically for the data structure you posted in the question and it works fine in the jsbin. Can you elaborate what you mean?
my first array "friends" got two sub array Salman razak and shahid saeed, in above for next loop, i get word friends two times because of the two sub arrays what if it show friends as parent and child both of the contact. hope you understand
Oh right, I think I get what you mean. You get the work "Friends" twice because I am logging the "group" they are in -- console.log(group, person.name); In this line the variable group is the name of the group they belong to, you dont have to use it if you dont want but I wanted to show that it was available to you.
0

try this one

   for (var i = 0, j = json.length; i < j; i++) {
    Object.keys(json[i]).forEach(function(elem, index, arr){
        for (var k = 0; k < json[i][elem].length; k++) {
            console.log(json[i][elem][k].image); 
            console.log(json[i][elem][k].unread);   
            console.log(json[i][elem][k].name);
            console.log(json[i][elem][k].message);
            console.log(json[i][elem][k].lastchat);
        }
    });
   }

// THIS LINE TO ITERATE THE MAIN ARRAY 'JSON'
for (var i = 0, j = json.length; i < j; i++) {

// THIS LINE TO GET KEYS OF json[i]......(Friends AND Colleagues FOR FIRST ELEMENT OF json)
Object.keys(json[i]).forEach(function(elem, index, arr){ 

//EACH KEY IS ANY ARRAY. THIS LINE TO ITERATE THE ELEMENTS OF EACH KEY
for (var k = 0; k < json[i][elem].length; k++) {

4 Comments

Dont use for loops, they introduce unnecessary complexity. Use forEach instead.
thanks intekhab you saved my day. its working perfectly alright
@RayonDabre In almost all cases. There're some special cases, but this is definitely not one if them.
based upon my categories i am fetching data of contacts and category showing them in <UL> <LI> and <UL> <LI> <UL>.

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.