There is the following query results: (key1 and key2 could be any text)
id key1 key2 value 1 fred apple 2 2 mary orange 10 3 fred banana 7 4 fred orange 4 5 sarah melon 5 ...
and I wish to store the data in a grid (maybe as an array) looping all the records like this:
apple orange banana melon fred 2 4 7 - mary - 10 - - sarah - - - 5
In PHP this would be really easy, using associative arrays:
$result['fred']['apple'] = 2;
But in JavaScript associative arrays like this doesn't work. After reading tons of tutorial, all I could get was this:
arr=[]; arr[1]['apple'] = 2;
but arr['fred']['apple'] = 2;
doesn't work. I tried arrays of objects, but objects properties can't be free text. The more I was reading tutorials, the more I got confused...
Any idea is welcome :)
PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.
A multidimensional array is an array that contains another array. For example, // multidimensional array const data = [[1, 2, 3], [1, 3, 4], [4, 5, 6]];
JavaScript does not provide the multidimensional array natively. However, you can create a multidimensional array by defining an array of elements, where each element is also another array. For this reason, we can say that a JavaScript multidimensional array is an array of arrays.
Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys. They do not have a length property like normal array and cannot be traversed using normal for loop.
Just use a regular JavaScript object, which would 'read' the same way as your associative arrays. You have to remember to initialize them first as well.
var obj = {}; obj['fred'] = {}; if('fred' in obj ){ } // can check for the presence of 'fred' if(obj.fred) { } // also checks for presence of 'fred' if(obj['fred']) { } // also checks for presence of 'fred' // The following statements would all work obj['fred']['apples'] = 1; obj.fred.apples = 1; obj['fred'].apples = 1; // or build or initialize the structure outright var obj = { fred: { apples: 1, oranges: 2 }, alice: { lemons: 1 } };
If you're looking over values, you might have something that looks like this:
var people = ['fred', 'alice']; var fruit = ['apples', 'lemons']; var grid = {}; for(var i = 0; i < people.length; i++){ var name = people[i]; if(name in grid == false){ grid[name] = {}; // must initialize the sub-object, otherwise will get 'undefined' errors } for(var j = 0; j < fruit.length; j++){ var fruitName = fruit[j]; grid[name][fruitName] = 0; } }
If it doesn't have to be an array, you can create a "multidimensional" JS object...
<script type="text/javascript"> var myObj = { fred: { apples: 2, oranges: 4, bananas: 7, melons: 0 }, mary: { apples: 0, oranges: 10, bananas: 0, melons: 0 }, sarah: { apples: 0, oranges: 0, bananas: 0, melons: 5 } } document.write(myObj['fred']['apples']); </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With