0

Here is my JavaScript:

var states = new Array();
var st = {'FL':{'profit':150}};
states.push(st);

alert(states[0]['FL']['profit']);

The problem with this is that I want to access variable like this alert(states['FL']['profit']); , meaning without [0] index... But that [0] index is added by default by "push" method. And I really need to append array into array this way because I am assigning through the loop. So is there way to append to array without assigning that index [0] ?

vitalii maximov
  • 89
  • 1
  • 11
  • Arrays have numeric indexes, objects have named properties. It sounds like you want to merge objects, not append to an array. – Barmar Feb 05 '15 at 22:13
  • ok, well, how do I do that? is there a way that I can set up things so that I can access my variable this way states['FL']['profit'] ? – vitalii maximov Feb 05 '15 at 22:14
  • Yes: `states = st;` or iterate over the `st` object, with a `for...in` loop and append each of the properties (the `'FL'`) and their values (the `{'profit':150}`) to a new object `st` (created by `st = {}`). But if you want them in an array you have to deal with the necessity of accessing the objects, and properties, via array (and object) notation. – David Thomas Feb 05 '15 at 22:16
  • @vitaliimaximov Isn't that answered in the duplicate question? – Barmar Feb 06 '15 at 00:12

0 Answers0