简体   繁体   中英

Javascript Creating an Array of Objects

I'm kind of new to Javascript. Any help or suggestion regarding below problem is highly appreciated.

I wanted to create an array which inturns contains list of objects. Given below code summarizes my problem

{
    var Instrument = {};
    var InstrumentArray = new Array;
    var array = new Array;
    array[0] ="XYZ0";
    array[1] ="XYZ1";
    .
    .
    .
    array[n] ="XYZn" ;
    data1['Name'] = "X";
    data1['TypeString'] = "WatchList";
    data1['FileTypeString'] = "XLS";
    for (var i = 0; i < array.length; i++) {

        Instrument['Symbol'] = array[i];
        InstrumentArray.push(Instrument);
    }
    for(var j =0; j< InstrumentArray.length;j++)
     {
         console.log(InstrumentArray[j]);
     }

   }

When I look into the output via console.log it displays me the correct number of values but the Symbol: value it displays is the last one that I have entered in this case "XYZn".

I know that the last value overrides the Symbol Object but is their any way I can get all the values stored.

Thanks in Advance

It's because each element in the array InstrumentArray is a reference to same object Instrument , and you keep changing the properties of that one object. There are many things to improve in your code, but the short answer is to create Instrument inside the loop.

In your code:

var Instrument = {};
var InstrumentArray = new Array;

While the formal parameter list can be omitted when calling a constructor with new , it is not a good idea as it might be misinterpreted as an assignment of Array rather than a new instance of Array. In any case, an array literal is likely a better choice (unambiguous, less to type):

var InstrumentArray = [];

Same for:

 var array = ["XYZ0", "XYZ1", ... "XYZn"]

In the following:

data1['Name'] = "X";
data1['TypeString'] = "WatchList";
data1['FileTypeString'] = "XLS";

data1 has not been declared or initialised, presumably it is an object (don't mix single and double quotes unless they are nested, it makes the code confusing) so a literal is likely better (same reasons as above):

var data1 = {'Name': 'X', 'TypeString': 'WatchList', 'FileTypeString': 'XLS'};

which is also a lot less to type.

 for (var i = 0; i < array.length; i++) {
      Instrument['Symbol'] = array[i];
      InstrumentArray.push(Instrument);
 }

Others have told you what is going on here, likely not what you want. Note that where property names comply with the rues for valid identifiers, dot notation can be used (which is a little more convenient):

      Instrument.Symbol = array[i];

Also, since you are adding members to an empty array using a counter, you can also do:

      InstrumentArray[i] = Instrument;

but of course they won't fix your issue, they are just different (better?) ways of doing the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM