Jquery creating two dimensional array

Edit: It looks like I was a bit confused about what I was trying to achieve. For those who took the time to explain this, thanks.

I am trying to create a two dimensional array in jQuery / Javascript. I did a decent amount of searching, testing, and searching, but I can't find a solution that really makes sense to me. (it was a very long week already ...)

Below is the desired array format.

{"product":[{"attribute":"value","attribute":"value"}]} 
+7
source share
5 answers

This is not a two-dimensional array, but rather an object. In addition, 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 create an array inside the product property:

 {"product":[{"attribute":"value"}, {"attribute":"value"}]} 
+21
source

You cannot create a two-dimensional array in Javascript, arrays can have only 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 required format that you are showing is neither a two-dimensional array nor a jagged array, it is an object that contains a property that is an array of objects. However, an object in an array has two properties with the same name, so I assume that you meant the presence of two objects in the array:

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

You can create a similar object using a literal object, as described above, or create it by adding properties and elements of the array:

 var o = {}; o.product = []; o.product.push({ attribute: "value" }); o.product.push({ attribute: "value" }); 
+14
source
 $(".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++ }); 
+2
source

Try the following:

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

This is my decision.

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

All Articles