开发者

jquery creating two dimensional array

Edit: It appears I was a bit confused on what I was trying to accomplish. For those that took the time to explain this, thank you.

I'm trying to create a two dimensional array in Jquery/Javascript. I've done a decent amount of searching, testing and more searching but i'm unable to find a solution that really makes sense to me. (it's been a very long week already....)

Belo开发者_如何学编程w is the desired format of the array.

{"product":[{"attribute":"value","attribute":"value"}]}


That's not a 2D array, but rather an object. Also, your product array contains only one object. I think you need something like this:

var obj = {};
obj.product = [];
for(var i=0; i < someObj.length; i++) {
   obj.product.push[{"attribute": someObj[i]}]
}

This will produce an array inside the product property:

{"product":[{"attribute":"value"}, {"attribute":"value"}]}


You can't create a two dimensional array in Javascript, arrays can only have one dimension. Jagged arrays, i.e. arrays of arrays, are used instead of two dimensional arrays.

Example:

var a = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

The desired format that you show is neither a two dimensional array nor a jagged array, instead it's an object containing a property that is an array of objects. However, the object in the array has two properties with the same name, so I assume, you meant that as having two objects in the array:

var o = {
  product: [
    { attribute: "value" },
    { attribute: "value" }
  ]
};

You can create an object like that using a literal object like above, or you can create it by adding properties and array items afterwards:

var o = {};
o.product = [];
o.product.push({ attribute: "value" });
o.product.push({ attribute: "value" });


Try this:

{"product":[ [{"attribute":"value"},{"attribute":"value"}]]} 


$(".adddiv").each(function(){

    tasks = [];
    $(".subtasktask"+len).each(function() {
        var raw = $(".subtasktask"+len).children().size();
        for(var l =0;l

        datas.push(milestone);
        alert("now show json milestone array : ");
        alert(milestone.month + ":" + milestone.title +":" + milestone.task. );
        len++
    });


This is my solution.

var optionArr = []
optionArr = {"product": [{"id":1, "name":"abc"}, {"name":"value"}]} 
var data = optionArr['product'][0]['name']
alert(data)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜