Claes Gustavsson
Claes Gustavsson

Reputation: 5679

How to loop an array in Javascript and group by 3?

I have an array where I need to group by 3 values within the loop. So first I need 3 values and then loop and get the 3 next values etc.

In the code below I need to read the first 3 array values and loop through the code and then get the next 3 array values.

The data is like so: title1,text1,image1,title2,text2,image2,title3,text3,image3. and is a callback from my server.

var myString = data;
var arr = myString.split(',');

var notisTitle = arr[0];
var notisMessage = arr[1];
var notisImage = arr[2];

// I need to loop this - first with the first 3 array values
// and the next time with the next 3 array values etc.
myApp.addNotification({
    title: notisTitle,
    message: notisMessage,
    media: '<img width="44" height="44" style="border-radius:100%;margin-top:-10px;" src="'+notisImage+'">',
    closeOnClick: true,
    onClose: function (data) {
    }
});

The "data" is a callback from my server (asp classic) and it gets "content" from below.

datan = Array (notisTitle, notisMessage, notisImage)
for i = 0 to ubound(datan)
content =  datan(0) & "," & datan(1) & "," & datan(2) & ","
next
response.write content

Upvotes: 2

Views: 1235

Answers (4)

BenG
BenG

Reputation: 15154

this should do what you need:-

var myString = data;
var arr = myString.substring(0, myString.length - 1).split(','); // remove last comma

for (var i = 0, l = arr.length; i < l; i++) {

  var notisTitle = arr[i];
  var notisMessage = arr[++i];
  var notisImage = arr[++i];

  //I need to loop this - first with the first 3 array values 
  //and the next time with the next 3 array values etc...         
  myApp.addNotification({
      title: notisTitle,
      message: notisMessage,
      media: '<img width="44" height="44" style="border-radius:100%;margin-top:-10px;" src="' + notisImage + '">',
      closeOnClick: true,
      onClose: function(data) {

    }
  });
}

UPDATE

if data sets myString like so:-

var myString = 'App Notis 2,Notis text 2,http://www.manmade.se/appmanager/admin/user_images/skolappen/splash/196x196.png,App Notis 3,Notis text 3,http://www.manmade.se/appmanager/admin/user_images/skolappen/notiser/background_webb.jpg,';

Then you will have 7 items in the array when splitting on ,. As you have a , on the end which gives index 6 "".

When you said:-

Thanks BG101, but the first time it loops the "myApp.addNotification" the values are empty?

I think your problem is this, but its not the first but last iteration.

remove the , from the end of your string and try.

UPDATE 2

the last comma can be removed like so:-

myString.substring(0, myString.length - 1).split(',');

see above.

Upvotes: 2

Raskolnikov
Raskolnikov

Reputation: 3999

Try this:

for (i = 0; i<arr.length; i = i + 3) { 
  var first = arr[i];
  var second = arr[i+1];
  var third = arr[i + 2];
}

Upvotes: 0

Penchalaiah
Penchalaiah

Reputation: 66

I think following code might suite your requirement

var arr = [1,2,3,4,5,6,7,8,9];
var len=arr.length;

for(var i=0;i<len;i=i+3) {
  var slicedArr = arr.slice(i,i+3);
  console.log(slicedArr);
}

Upvotes: 0

AlexBay
AlexBay

Reputation: 1313

var myString = data;
var arr = myString.split(',');

for (var i = 0; i < arr.length; i + 3) {
  var notisTitle = arr[i];
  var notisMessage = arr[i + 1];
  var notisImage = arr[i + 2];

  //I need to loop this - first with the first 3 array values 
  //and the next time with the next 3 array values etc...         
  myApp.addNotification({
    title: notisTitle,
    message: notisMessage,
    media: '<img width="44" height="44" style="border-radius:100%;margin-top:-10px;" src="' + notisImage + '">',
    closeOnClick: true,
    onClose: function(data) {

    }
  });
}

Consider removing assignments if you use them only once:

var myString = data;
var arr = myString.split(',');

for (var i = 0; i < arr.length; i + 3)
  //I need to loop this - first with the first 3 array values 
  //and the next time with the next 3 array values etc...         
  myApp.addNotification({
    title: arr[i],
    message: arr[i + 1],
    media: '<img width="44" height="44" style="border-radius:100%;margin-top:-10px;" src="' + arr[i + 2] + '">',
    closeOnClick: true,
    onClose: function(data) {

    }
  });

Upvotes: 1

Related Questions