2

I've been searching and searching and haven't found a solution...even though, likely, it's simple. How do I create something that will give me this:

myArray['key1'].FirstName = "First1";   
myArray['key1'].LastName = "Last1";   
myArray['key2'].FirstName = "First2";  
myArray['key2'].LastName = "Last2";  
myArray['key3'].FirstName = "First3";   
myArray['key3'].LastName = "Last3";

And then say something like, alert(myArray['key2'].FirstName);
And will I be able to iterate through it like:

for(i=0; i < myArray.length; i++){
    //do whatever
}

Thanks in advance!

0

4 Answers 4

3

You can init an object something like that:

{
    "key1": {FirstName: "first1", LastName: "last1"}
    "key2": {FirstName: "first2", LastName: "last2"}
    "key3": {FirstName: "first3", LastName: "last3"}
}

Sample function for init your array:

function initArray(){
  for(var i=1; i< count+1; i++) {
        var newElement = {}
    newElement.FirstName = "first" + i;
    newElement.LastName = "last" + i;
    var keyName = "key" + i
    var obj = {};
    myArray[keyName] = newElement
    }
}

Now "myArray["key2"] is accessible.

http://jsfiddle.net/jq5Cf/18/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! The first part of your answer really set me on the right path! I Here's what seems to work like I need:
'//Setup the initial elements in the array myArray = { "key1": {FirstName: "first1", LastName: "last1"}, "key2": {FirstName: "first2", LastName: "last2"}, "key3": {FirstName: "first3", LastName: "last3"} }; //Add to the array myArray["key4"] = {FirstName: "first4", LastName: "last4"}; //Loop through and display the FirstName of each element in the array for(i=0; i < Object.keys(myArray).length; i++){ alert(myArray[Object.keys(myArray)[i]].FirstName); //Wonder if there is a short way to do this line }'
Thanks again @a.u.b As I mentioned on another answer above, when I think more, what I really need is something like this: myArray[theKey][theIndex].FirstName;' and then loop through it like: for(i=0; i < myArray[theKey].length; i++).... So myArray['key1'][0].FirstName = 'Fred';` etc
You're welcome. I'm glad to find the right path. :) Could you add these codes to your question? (And pls vote me. :))
1

You can't do what you're trying to do in javascript! (because javascript can't do associative arrays)

I would go for an object which has an internal array to store other things

var container = {};
container.things = [];
container.things.push({FirstName: 'First1', LastName: 'Last1'});

now you can do..

for(var i in container.things) {
    alert(container.things[i].FirstName);
}

Comments

1

In JavaScript we use arrays like this, [] for Arrays and Objects are in {}

var MyArray = [
               {FirstName: "Firsname1" , LastName: "Lasname1"},
               {FirstName: "Firsname2" , LastName: "Lasname2"}
              ]

2 Comments

Thanks for answering, but I'm not sure it would work for what I need...when I think more, what I really need is something like this: myArray[theKey][theIndex].FirstName;' and then loop through it like: for(i=0; i < myArray[theKey].length; i++).... So myArray['key1'][0].FirstName = 'Fred';` etc
I think you will get your anwer here without using i you can use for key in array loop
0

Your myarray variable construction is in notation of objects of objects.

var myArray = {'key1':
    {
        'FirstName' : "First1",
      'LastName' : "Last1"
    }};

In order to access the values should be like array of objects.

var myArray = [
    {
    'FirstName' : "First1",
      'LastName' : "Last1"
    },

];

or notation can be like below:

var data = {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.