0

I need to create object parameter some thing like this

sample_data=[{
                        name: 'new',
                        data: [1, 0, 2, 8, 1]
                    }, {
                        name: 'open',
                        data: [1, 2, 3, 7, 1]
                    },{
                        name: 'closed',
                        data: [5, 4, 3, 1, 1]
                    },{
                        name: 'onhold',
                        data: [1, 1, 2, 0, 1]
                    }, {
                        name: 'completed',
                        data: [0, 0, 5, 1, 2]
                    }];

I had totally 6 arrays all the name parameters in array1 and all data parameters in another arrays.

Is there any option to built object as my requirement above, using arrays that i have

My arrays:

name_array=Array("new","open","closed","onhold","completed");

    new_data=Array(1, 0, 2, 8, 1);
    open_data=Array(1, 2, 3, 7, 1);
    closed_data=Array(5, 4, 3, 1, 1);
    onhold_data=Array(1, 1, 2, 0, 1);
    completed_data=Array(0, 0, 5, 1, 2);

Help me how to create object using these array in javascript or jquery.

1
  • Since there are a flurry of similar answers, I'm just going to post this here as a reference. jsfiddle.net/mkmcdonald/RJSZ9/1 Commented Mar 11, 2011 at 5:31

5 Answers 5

3

This solution assumes your array objects are declared globally (on the window object). Typically I would not suggest this type of thing, but this works:

var sampleData = [];

for (var i = 0; i < name_array.length; i++) {
    var arrayName = name_array[i];
    sampleData.push({
        name: arrayName,
        data: window[arrayName + "_data"]
    });
}

Here is a jsfiddle with it working:

http://jsfiddle.net/magicaj/HzQvu/

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

Comments

0

It sounds like what you should be using is an object.

names = {
  "new" : Array(1, 0, 2, 8, 1),
  "open" : Array(1, 2, 3, 7, 1),
...
};

So to construct it given the sample_data above would be:

names = {};
for(var i =0; i<sample_data.length; i ++){
  names[sample_data[i].name] = sample_data[i].data;
}

You can then access the object as names["new"] or var which = "new"; names[which]; and so on, and iterate over the object if needed.

Comments

0

With the eval() function...

var name_array=Array("new","open","closed","onhold","completed");

 var   new_data=Array(1, 0, 2, 8, 1);
   var  open_data=Array(1, 2, 3, 7, 1);
   var  closed_data=Array(5, 4, 3, 1, 1);
   var  onhold_data=Array(1, 1, 2, 0, 1);
  var   completed_data=Array(0, 0, 5, 1, 2);

$(function(){

  var combined = [];  
  for(var i = 0;i<name_array.length;i++)
  {
      combined.push({
          name: name_array[i],
          data: eval(name_array[i] + "_data")
      });
  } 


});

http://jsfiddle.net/tbErD/

Comments

0

You can get shorter code using jQuery.map.

var sample_data = $.map(name_array, function(value, index) {
  return {name: value, data: eval(value + '_data')};
});

Comments

0

$.map() is a clean, expressive way to do that:

var sample_data = $.map(name_array, function(type) {
  return { 
    name: type, 
    data: window[type + '_data']
  };
});

Whether you use a for-loop or $.map() (which is a for-loop under the hood too), it's important to distinguish between using window[string] and eval(string). Building a string to use as a window key is not the same thing as using eval and doesn't share its vulnerabilities.

For example, window['alert("doevil")'] harmlessly returns undefined.

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.