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);
10
  • There are no associative arrays in javascript, so good luck with that. Look into objects instead! Commented Nov 29, 2012 at 17:13
  • What is the question? What are you having problems with? You might find reading MDN - Working with Object helpful. Commented Nov 29, 2012 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. Commented Nov 29, 2012 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. Commented Nov 29, 2012 at 17:15
  • @FelixKling, yes, thats what I mean. Thanks for the correction. Commented Nov 29, 2012 at 17:16

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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: 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.
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.
@Diego accepted your answer since it lead me to the actual solution.
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));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.