开发者

How to use object in javascript or jquery

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 开发者_如何学Pythonoption 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.


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/


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.


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/


You can get shorter code using jQuery.map.

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


$.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜