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.

6
  • 2
    So what is the question? Commented Jan 5, 2014 at 2: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. Commented Jan 5, 2014 at 2:26
  • if the b array have dynamic elements let says 1000 element how we will run the concat function Commented Jan 5, 2014 at 2:26
  • 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? Commented Jan 5, 2014 at 2: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. Commented Jan 5, 2014 at 2:37

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

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.