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!

Hawker
  • 23
  • 1
  • 1
  • 7

4 Answers4

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/

a.u.b
  • 1,589
  • 10
  • 25
  • 1
    Thanks! The first part of your answer really set me on the right path! I Here's what seems to work like I need: – Hawker Feb 07 '17 at 14:42
  • 1
    '//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 }' – Hawker Feb 07 '17 at 14:44
  • 1
    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 – Hawker Feb 07 '17 at 15:18
  • You're welcome. I'm glad to find the right path. :) Could you add these codes to your question? (And pls vote me. :)) – a.u.b Feb 07 '17 at 16:01
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);
}
Dale
  • 10,384
  • 21
  • 34
1

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

var MyArray = [
               {FirstName: "Firsname1" , LastName: "Lasname1"},
               {FirstName: "Firsname2" , LastName: "Lasname2"}
              ]
G. Mansour
  • 696
  • 6
  • 15
  • 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 – Hawker Feb 07 '17 at 15:13
  • I think you will get your anwer [here](http://stackoverflow.com/questions/1144705/best-way-to-store-a-key-value-array-in-javascript) without using `i` you can use `for key in array loop` – G. Mansour Feb 08 '17 at 07:37
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'
    }]
};
Indhu
  • 371
  • 1
  • 5
  • 18