Javascript jquery创建二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6933096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:50:06  来源:igfitidea点击:

jquery creating two dimensional array

javascriptjqueryarraysmultidimensional-array

提问by Stephen S.

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

我正在尝试在 Jquery/Javascript 中创建一个二维数组。我已经做了大量的搜索、测试和更多的搜索,但我无法找到对我来说真正有意义的解决方案。(这已经是很长的一周了......)

Below is the desired format of the array.

以下是所需的数组格式。

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

回答by Mrchief

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 productproperty:

这将在product属性内生成一个数组:

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

回答by Guffa

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.

您不能在 Javascript 中创建二维数组,数组只能有一个维度。锯齿状数组,即数组的数组,用于代替二维数组。

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" });

回答by Bharat Desai - Certified Dev

$(".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++
    });

回答by SAWAUNG

This is my solution.

这是我的解决方案。

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

回答by Chandu

Try this:

尝试这个:

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