0

Trying to create the following:

array( '12345' => 'A01', 'A02', 'A03'
'22222' => 'B01',
'33333' => 'C01', 'C02')

So basically each key is different generated dynamically from another array. Let's say variable numbers has '12345' after certain event is fired.

We have an array called location, this one will have for example ('A01', 'A02', 'A03')

So then on a master array it will map both numbers with the location. This is the array that i will need to save each time there is an event.

The on the next event execution we shall get a new value on numbers variable such as '22222' and then a new array location will overwrite the other one with ('B01') for example and so on.

Remember the keys are gonna be always dynamic and the values can be from 1 to 50 for example we don't know. I know this is more like Object Literals on Jquery. thx in advance.

Here is the piece of code, need to be able to get the key and values

             $.each(dragarray, function(index, value) {

                    dragid_loc['value'] = [];
                    // do loop to add each element of other array
                    $.each(draglocation, function(index2, value2) {
                        dragid_loc.value.push(value2);
                    });

            });

            console.log(dragid_loc);

This line seems to cause the problem i won't push the values of another array draglocation into each. Need to get the key and the value.

dragid_loc.value.push(value2);
Hard Fitness
  • 452
  • 3
  • 9
  • 24
  • There are no associative arrays in javascript, so good luck with that. Look into objects instead! – adeneo Nov 29 '12 at 17:13
  • What is the question? What are you having problems with? You might find reading [MDN - Working with Object](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects) helpful. – Felix Kling Nov 29 '12 at 17:14
  • You should use json instead of associative arrays. On each time the event is fired you will add the key and the value to the json. It's not clear how to get that data. – Diego Nov 29 '12 at 17:14
  • @Diego: JSON is a data exchange format, it has nothing to do with JavaScript (it just looks similar to JS object literals). I assume you mean object. – Felix Kling Nov 29 '12 at 17:15
  • @FelixKling, yes, thats what I mean. Thanks for the correction. – Diego Nov 29 '12 at 17:16
  • @FelixKling I am having problems creating an object that has the form inside the code box i posted. I have used JSON before and was thinking of doing that but not sure on how to get those elements on my loop to use them. – Hard Fitness Nov 29 '12 at 17:24
  • @Diego I am used to seeing JSON as {"id": "12345", "location": "A01"} but how would you add more items to location? Remember each location can have 1 to 50 items. So not sure if {"id": "12345", "location": "A01", "A02", "A03"} would work or how to get that into a JSON format. – Hard Fitness Nov 29 '12 at 17:31
  • @HardFitness, no. What you should do is: `{"id": "12345", "location": ["A01", "A02", "A03"]}` – Diego Nov 29 '12 at 17:40
  • @Diego how would I add that to a JSON format using arrays? And how would access all those elements on the jquery program? I would need to individually access A01, A02 and A03 and do something with each of them. – Hard Fitness Nov 29 '12 at 17:57
  • If you say "JSON", do you mean actual JSON or an object literal? If you have an object, just assign an array to `location`: `obj.location = [...];` and you access arrays with `[X]` where `X` is a positive integer: `obj.location[2]`. – Felix Kling Nov 29 '12 at 18:52

2 Answers2

6

Based in the comments I think what you need is:

  • obj["newProp"] = []; // A new property is added to the object with key newProp and an empty array as value
  • obj.newProp.push(newElement); // A new element is added to the array in newProp of object
Diego
  • 16,436
  • 26
  • 84
  • 136
  • I will edit my answer and give the piece of code that has the this problem. Maybe that way it will be easier, what I also need is to know is how to access the key and values of each. – Hard Fitness Nov 29 '12 at 20:28
  • the printing of the element on console.log(dragid_loc) gives me as the key "value". And i don't want that. I need to have the actual numeric element '12345' as the key. – Hard Fitness Nov 29 '12 at 21:33
  • @Hard: `dragid_loc[value2] = ....`. Including code always makes it easier for us to provide you with better answers. It's still not clear where the values you want to put into the array are coming from. – Felix Kling Nov 29 '12 at 22:32
  • i was able to get this working using dragid_loc['id'] = []; dragid_loc['location'] = []; and then pushing a number on 'id' and loop thru an array and push into 'location' [] id [ "49514" ] location [ "F39" ,"F40" ] Still wanna do one more thing which is create a huge array that stores all these values and then I wanna be able to to search thru it. But that would be a separate question i think. – Hard Fitness Nov 29 '12 at 23:08
  • @Diego accepted your answer since it lead me to the actual solution. – Hard Fitness Nov 29 '12 at 23:10
2

var Obj={}

var val1='12345';

Obj[val1]={0:'A01',1:'A02',2:'A03'};

var val2='22222';

Obj[val2]={0:'B01'};

alert(JSON.stringify(Obj));

Mitali
  • 2,187
  • 1
  • 15
  • 6