1

I want to get random hotelCode (for example CUNMXSAKU but randomly) object. Is it possible to have this object randomly in javaScript or Jquery.

My JSON:

    var simulatedHotelCodes = {
            "CUNMXSAKU" : {
                "roomCodes" : "DEAL, JRST, JPOV, JSSW, PJRS, PJOV, PJSW, RMOV, RMOF, PRES"
            },
            "CUNMXMAYA" : {
                "roomCodes" : "ROAI, FVAI, DXAI, CAAI, SUAI, CABA, SIGA, PRAI, POFA, ROOM, FMVW, DELX, CASA, SUIT, CASI, SIGN, PROF, PROFS"
            },
            "CUNMXDPAV" : {
                "roomCodes" : "GDVW, MRNA, FMLY, DFAM, HNDO, OCVW, DOLP, FMOV, PCDO, HNOC, PCOV, PFOV, ROHO"
            },
            "CUNMXHIDD" : {
                "roomCodes" : "JRST, JRSU, DOME"
            },
            "CUNMXDSAN" : {
                "roomCodes" : "DEAL, DELX, DEXBA, DXOF, DOFB, PROV, PROF, PROB, POFC, HONY, FAMI, PRJS, DEBL, PRDD"
            }
    };

Output:

"CUNMXMAYA" : {
                "roomCodes" : "ROAI, FVAI, DXAI, CAAI, SUAI, CABA, SIGA, PRAI, POFA, ROOM, FMVW, DELX, CASA, SUIT, CASI, SIGN, PROF, PROFS"
            }

or

"CUNMXHIDD" : {
                "roomCodes" : "JRST, JRSU, DOME"
            }

Randomly Thanks in advance

3 Answers 3

4

I'd use Object.getOwnPropertyNames() to get an array of all the properties, then pick a random index.

var simulatedHotelCodes = {
  "CUNMXSAKU" : {
    "roomCodes" : "DEAL, JRST, JPOV, JSSW, PJRS, PJOV, PJSW, RMOV, RMOF, PRES"
  },
  "CUNMXMAYA" : {
    "roomCodes" : "ROAI, FVAI, DXAI, CAAI, SUAI, CABA, SIGA, PRAI, POFA, ROOM, FMVW, DELX, CASA, SUIT, CASI, SIGN, PROF, PROFS"
  },
  "CUNMXDPAV" : {
    "roomCodes" : "GDVW, MRNA, FMLY, DFAM, HNDO, OCVW, DOLP, FMOV, PCDO, HNOC, PCOV, PFOV, ROHO"
  },
  "CUNMXHIDD" : {
    "roomCodes" : "JRST, JRSU, DOME"
  },
  "CUNMXDSAN" : {
    "roomCodes" : "DEAL, DELX, DEXBA, DXOF, DOFB, PROV, PROF, PROB, POFC, HONY, FAMI, PRJS, DEBL, PRDD"
  }
};

var properties = Object.getOwnPropertyNames(simulatedHotelCodes);
var index = Math.floor(Math.random() * properties.length);
var output = {};
output[properties[index]] = simulatedHotelCodes[properties[index]];
console.log(output);

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know about Object.getOwnPropertyNames() It seems very usefull. However keep in mind that not all browsers support it.
0

If you only care about modern browsers, check @dave's answer which is cleaner (and it probably has a better performance). If you want a cross-browser solution here there is an option:


// Define amount of json objects
var length = 0;
for (var key in json)
    length++;
// Get random index and iterate until get it
var rnd = Math.floor(Math.random()*length),
    i = 0,
    obj;
for (var key in json) {
    if (i == rnd) {
        obj = {};
        obj[key] = json[key];
        break;
    }
    i++;
}
// obj has the random item

Note that you would need the random selected key to use obj. You could change this:

obj = {};
obj[key] = json[key];

to this:

obj = json[key];

to save only the random item value.

http://jsfiddle.net/paska/m2y7gvnp/1/

Comments

0

Yes it is possible. But it would be more easy if you put those object in array so that you can access those object through array index. As array index is an integer, you can randomly generate that array index and then access that object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.