0

I'm trying to merge multiple arrays evenly/alternating in javascript/Google appScript. There are several arrays (5 or 6). I've tried 2 different methods, but neither worked. I don't work a lot with javascript honestly and I've managed to get the code to this point, but I can't get it merge properly; and most of them said merging two arrays to one (might be my problem).

I've seen plenty on php examples that were on how to do this and they are pretty straight forward in logic reading and I understand them better, but all javascript methods I've looked at and tried so far have failed to produce the results I want. I'm not sure if it's the way AppScript is formatting the arrays or they're just no made to handle more that 2.

My data looks similar to this at the moment:

var title = ["sometitle1","sometitle2","sometitle3"];
var link = ["somelink1","somelink2","somelink3"];
var date = ["somedate1","somedate2","somedate3"];
var data = ["somedata1","somedata2","somedata3"];
var all = [title,link,date,data];
var mix = [];

Note: all the variable data will/should be the same length since the data is being pulled from a spreadsheet.

My desired output is:

mix = ["sometitle1","somelink1","somedate1","somedata1","sometitle2","somelink2","somedate2","somedata2","sometitle3","somelink3","somedate3","somedata3"];

I tried using appscript to merge them with this: return ContentService.createTextOutput(title + link + data + date), but it didn't work out properly, it printed them in that order instead of merging the way I'd like them too.

Then I tried using a loop merge that I found here on sstackoverflow:

for (var i = 0; all.length !== 0; i++) {
    var j = 0;
    while (j < all.length) {
        if (i >= all[j].length) {
            all.splice(j, 1);
        } else {
            mix.push(all[j][i]);
            j += 1;
        }
    }
}

But it splice merges every letter with a comma

mix = [s,o,m,e,t,i,t,l,e,1,s,o,m,e,t,i,t,l,e,2,s,o,m,e,t,i,t,l,e,3,s,o,m,e,l,i,n,k,1,...]

and doesn't alternate data either.

The code (2 version) I'm working on is: here with Output & Here with Output

(Also, dumb question, but do I use title[i] + \n OR title[i] + "\n" for adding new lines?)

1 Answer 1

2

Use a for loop and the push() method like this :

function test(){
  var title = ["sometitle1","sometitle2","sometitle3"];
  var link = ["somelink1","somelink2","somelink3"];
  var date = ["somedate1","somedate2","somedate3"];
  var data = ["somedata1","somedata2","somedata3"];
  //var all = [title,link,date,data];
  var mix = [];
  for(var n=0;n<title.length;n++){
    mix.push(title[n],link[n],date[n],data[n]);
  }
  Logger.log(JSON.stringify(mix));
}

enter image description here

And also : title[i] + "\n" for adding new lines


Edit following comments :

Your code should end like this :

  ...
  for(var n=0;n<titles.length;n++){
    mix.push(titles[n],links[n],descriptions[n],pubdates[n],authors[n]);
  }
  var mixString = mix.join('');// convert the array to a string without separator or choose the separator you want by changing the argument.

  //Print data and set mimetype
  return ContentService.createTextOutput(mixString)
  .setMimeType(ContentService.MimeType.RSS);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Is there a way to remove the injected comma? If you set it to mix.push(title[n] + link[n] + date[n] + data[n]); it puts a comma before title2 & 3
there are actually no commas, this is how array elements are represented, separated by commas. They appear if you convert the array to strings. what are you wanting to get ? This returns exactly what you asked in your question.please show an example of how you want it to look like...
Sorry, this works like I asked; it's the exact answer needed for the question posed and thank you. For some reason it doesn't work on my data project though :(. For some reason it just outputs comma seperated letters/symbols still. Script: pastebin.com/VHfcYEFh & Output: pastebin.com/hyvEK49A
You are trying to add elements to an array using += (which I think changes your array to a long string...? not sure but from what you say it seems to behave like a string), use push method instead. titles.push("<item>" + "<title>" + title[i] + "</title>" + "\n");
HA! Yes! That fixed it! For some reason there are still like 18 commas though lol. They precede "<item>" starting at #2 all the way to the last one
|

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.