0

the follwoing code

$.concat||$.extend({concat:function(b,c){var a=[];for(x in arguments)a=a.concat(arguments[x]);return a}}); 

$(document).ready(function(){

    //document ready

    var solobroadcast_template =$("#hidden_solobroadcast").html()
    var b = new Array();   
    b.push([{"indexname": "green", "url":"#Green"}])
    b.push([{"indexname": "red", "url":"#red"}])    
   //....... more elements come here 

   //how can we know b[] while the array b contain unknown keys
   var bindexes = $.concat(b[0],b[1]);   



    var convertedvars = {
        name: "sam",
        indexes: function (){return bindexes}

    }


    var output = Mustache.render(solobroadcast_template, convertedvars);
    document.getElementById("content").innerHTML = output;
});

as it runs on http://jsfiddle.net/mshannaq/ZqqMe/6/

as you can see the variable bindexes = $.concat(b[0],b[1]); and in this case its static for b[0] and b[1] but what shloud we do if we want to to cocat all the b array size . imagin that b array size may be 1000 element and its dynamic.

Mohammed Shannaq
  • 806
  • 3
  • 8
  • 21
  • 2
    So what is the question? – zerkms Jan 05 '14 at 02:24
  • 1
    You could easily go through 1000 dynamic elements in nano seconds with a for loop. I don't think you need to optimize. The general rule is not to optimize until you have witnessed slowdowns. – Damien Black Jan 05 '14 at 02:26
  • if the b array have dynamic elements let says 1000 element how we will run the concat function – Mohammed Shannaq Jan 05 '14 at 02:26
  • what if I don't know what is b size because it comes from ajax how I will do the concat function? – Mohammed Shannaq Jan 05 '14 at 02:27
  • 1
    @DamienBlack "The general rule is not to optimize until you have witnessed slowdowns." Says who? How will you ever learn to optimize (or write secure code) unless you think about optimization (and security) with each and every line of code you write? – Joseph Myers Jan 05 '14 at 02:32
  • 1
    I don't understand why you're creating `b[]` as an array of arrays. As you have it, you're pushing an array of one object onto each element. Why not just push the object, then no concatenation will be required. –  Jan 05 '14 at 02:37

2 Answers2

1

Just add objects to the array instead adding the array:

b.push({"indexname": "green", "url":"#Green"})
b.push({"indexname": "red", "url":"#red"})

var convertedvars = {
    name: "sam",
    indexes: function (){return b;}
}

Then you already have an array of what you need, instead of concatenating all your smaller arrays.

Demo: http://jsfiddle.net/ZqqMe/11/

Dennis
  • 32,200
  • 11
  • 64
  • 79
0

in this case to fix this we use

 var bindexes = [];
 bindexes = bindexes.concat.apply(bindexes, b);

insted of using

var bindexes = $.concat(b[0],b[1]);

and now what ever how much b array elements they will be mereged

http://jsfiddle.net/mshannaq/ZqqMe/8/

as says on Merge/flatten an array of arrays in JavaScript?

Community
  • 1
  • 1
Mohammed Shannaq
  • 806
  • 3
  • 8
  • 21