jquery - arrange array in a "checkerboard" pattern based on type using javascript and/or underscore -


TWEET | INSTAGRAM

INSTAGRAM | Tweet it

TWEET | INSTAGRAM

INSTAGRAM | Tweet

The amount of tweets in the array is much higher than the amount of photos in the instma, but I have to make sure that regardless of the number of items in the feed, the results are optional in this fashion. How can I rearrange the array with the most efficient, as if I keep this method? I can only use JavaScript, jquery or underscores.

To start, you must apply a couple of JS functions as follows:

  function getElements () {// can be implemented by you, gives an array} function element ItsTweet (elem) {// you have to implement, give a boolean} < / Code> 

A few of the assumptions that were made here are that we leave processing after the tweets or the Instagram Programming is encouraged. It should also be noted that intermediate results are collected in the array. For larger sets, it is likely to be more efficient to track the number of indexes during the formation of the resulting array. But the code has started to mess so much :)

  // This gives a single-dimensional array, // rotate between Tweets and instalagram function getalternatingArray () {var arr = GetElements (); // Break Array var tweets in different parts = [], instagram = []; {If (elementIsTweet (arr [i])) for {var i = 0; i & lt; arr.length; i ++} {tweets.push (arr [i]); } Other {instagrams.push (ARE [Ii]); }} // re-combine var newArr = []; Var isTweet = (Math.Randon () & lt.5); While (true) {var currArr = istweet? Tweets: Instagram; If (currArr.length == 0) {break; } NewArr.push (currArr.shift ()); IsTweet =! ITunes; } Return new; }  

Finally, once you have a "checkboard" after a one-dimensional array with option type items, it is very trivial to create a multi-dimensional array.

  Function checker borarerae (column) {// To verify the error, that column must also be valid * Even * var arr = getAlternatingArray (); Var newArr = []; (Var i = 0; i * column  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -