How to define an array in an object that does not have one?

advertisements

I want to end up with something like:

{
Link:
    [
        {
            'Key':String,
            'Versions':[]
        }
    ]
}

Where Key is dynamic and not a string called Key.

For example, this could be:

{
    Link:[
        {
            'Hello':'Hi!',
            'Versions':[1,2,3]
        },
        {
            'Foo':'Bar',
            'Versions':[5g32,6392]
        }
    ]
}

I want to fill in this object iteratively, using Array.push to fill in the values for Version, so my program needs to know that the value of Versions is an array.

How can I do this?

The other question linked here does not answer my question, as this is a single 1 to 1 relation.

I need a 1 to many relation.

I need one key to link to an array of values.

The source of the data that I want to pair comes from a single array, which is why I need to do this iteratively.

The keys and the values for Versions are all in this single array.

They're at least in order.

My data source would be something like:

[
 {
   Key:'Slide1',
   Version: 'FSDdfgonbwipoenv',
 },
 {
   Key:'Slide1',
   Version : 'DFpiesenfsgbv',
 },
 {
  Key:'Slide2',
  Version: 'SpShdpkins',
 }
]

So it would be easy to just iterate through the array and link the pieces of information together, so that they form

[
 {
  Key:'Slide1',
  Version: ['FSDdfgonbwipoenv','DFpiesenfsgbv']
 ...etc


EDIT: Now that you've included more info on what you're actually trying to do:

Your data structure is not very friendly to what you are trying to do. An array for Link is the wrong type of structure if every item is going to have a unique key. But, if that's what you want, then you have to search through the array to find the matching key:

// versions to add for given Key values
var newInfo = [
 {
   Key:'Slide1',
   Version: 'FSDdfgonbwipoenv',
 },
 {
   Key:'Slide1',
   Version : 'DFpiesenfsgbv',
 },
 {
  Key:'Slide2',
  Version: 'SpShdpkins',
 }
];

// original data structure
var data = {Link: []};

// function to take an array of objects in newInfo and add them to the
// data structure
function addVersions(info) {
    var link = data.Link;

    // find a given key in the Link array
    function findKey(key) {
        for (var i = 0; i < link.length; i++) {
           if (link[i].Key === key) {
               return link[i];
           }
        }
    }

    // go through each item in the new info
    for (var i = 0; i < info.length; i++) {
        // find the right key
        var o = findKey(info[i].Key);
        // if we didn't find the key, then create one
        // and add it to the data structure
        if (!o) {
            o = {Key: info[i].Key, Version: []};
            link.push(o);
        }
        // add this new version onto the array for the right key
        o.Version.push(info[i].Version);
    }

}


Original answer before question was edited to point to a different problem.

You can do this:

// construct shell of the object
var data = {Link: []};

// directly .push() a new object where everything is known statically
data.Link.push({Hello: 'Hi!', Versions: [1,2,3]});

// .push() a new object where you have to build the object from the contents
// of other variables
var key1 = "Foo";
var key2 = "Versions";
var data1 = "Bar";
var data2 = [5g32,6392];
var obj = {};
obj[key1] = data1;
obj[key2] = data2;
data.Link.push(obj);

console.log(data.Link.length);   // an array of two objects

The key is the this syntax for assigning a property via a name contained in a variable and not known at the time you write the code:

    obj[key1] = data1;

You will end up with this:

var data = {
    Link:[
        {
            'Hello':'Hi!',
            'Versions':[1,2,3]
        },
        {
            'Foo':'Bar',
            'Versions':[5g32,6392]
        }
    ]
};


Of, if you already have the Link array of objects and you want to add Versions, you can use .push() to add version numbers to the Versions array:

data.Link[0].Versions.push(4);

Would change the above data structure to this:

var data = {
    Link:[
        {
            'Hello':'Hi!',
            'Versions':[1,2,3, 4]
        },
        {
            'Foo':'Bar',
            'Versions':[5g32,6392]
        }
    ]
};

Or, if you want to dynamically iterate through the Link array, you could do this:

for (var i = 0; i < data.Link.length; i++) {
    data.Link[i].Versions.push(xxx);
}