简体   繁体   中英

How Do I Create Variable Object Keys Of Object Keys Of Objects?

Okay, so I have a search results array of objects where one of the object properties value (show value) matches a search. The structure of this array is as follows and may contain any number of different objects:

results = [
    {
        day: value,
        time: value,
        show: value,
        sid: value,
        network: value,
        title: value,
        ep: value,
        link: value,
    }
];

I am trying to consolidate all the results into one large object, merging any days or times that have the same value. However, I cannot simply look at each day or time value independently. For example, I need to retain 9:00 pm on Monday if there is a 9:00 pm on Tuesday as well.

To do this I am trying to create a new object structure like so:

for ( var i=0; i<results.length; i++ ) {
    var uniqtime = results[i]["time"];
    var uniqshow = results[i].show;
    uniqresults[results[i].day] = {
        uniqtime: {
             uniqshow: {
                sid: results[i].sid,
                network: results[i].network,
                title: results[i]["title"],
                ep: results[i].ep,
                link: results[i]["link"]
            }
        }
    };
}

but obviously this won't work since the variable sub-object key names are treated as strings.

If I instead try to create the variable sub-objects/keys like so:

for ( var i=0; i<obj.length; i++ ) {
    uniqresults[results[i].day] = {};
    uniqresults[results[i].day][results[i]["time"]] = {};
    uniqresults[results[i].day][results[i]["time"]][results[i].show] = {
        sid: obj[i].sid,
        network: results[i].network,
        title: results[i]["title"],
        ep: results[i].ep,
        link: results[i]["link"]
    };
}

I can indeed create the proper key names but I am forced to declare an empty object to define each key ( uniqresults[obj[i].day] = {} & uniqresults[obj[i].day][obj[i]["time"]] = {} ). If I don't declare like this it won't let me insert the other sub-keys/values that I need to. However, declaring like this doesn't allow me to merge my results arrays correctly since I am essentially emptying out the sub-key names each time I read a new result object!

Maybe I am making this more complicated than it should be. Maybe there is an easier way or a way underscore or jquery could simplify my task at hand. Regardless, I am quite desperate for a proper solution at the moment.

I don't know whether there's a neater solution to the wider problem, but the issue of overwriting the sub-objects each time can be solved by checking if they already exist before creating them.

A relatively compact and idiomatic way of doing this in JS is using the || operator, which unlike most languages returns the argument which evaluated to true, not simply a boolean true :

uniqresults[results[i].day] = uniqresults[results[i].day] || {};

The first time through, uniqresults[results[i].day] will be undefined, so evaluate to false, so {} will be assigned; subsequently, however, it will be an object, which evaluates to true, so will simply assign the variable to itself, leaving it unchanged.

It seems like you could use a conditional check when redifining those objects.

var day, time;
for ( var i=0; i<obj.length; i++ ) {
    // Instantiate the day reference if it doesn't exist
    day = uniqresults[results[i].day] = uniqresults[results[i].day] || {};

    // Instantiate the time reference if it doesn't exist
    time = day[results[i].time] = day[results[i].time] || {};

    time[results[i].show] = {
        sid: obj[i].sid,
        network: results[i].network,
        title: results[i]["title"],
        ep: results[i].ep,
        link: results[i]["link"]
    };
}

Cheers!

Create an empty object only if the object's key does not exist:

if(!(results[i].day in uniqresults)){
   uniqresults[results[i].day] = {};
}

And for sub keys so on.

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