3

I have experience only really with JS arrays, not objects, so I'm trying to learn how to navigate them. I would appreciate any answers with good explanation that show the process of completing the following task:

I have a month (expressed as an integer, 1-11), and a collection of data for various IDs, (expressed as an integer too, e.g 474)

The data looks like this (a subset of some longer data):

var myData = {
  1: {474: true, 459: true},
  4: {474: true, 578: true, 987: true, 459: true, 917: true, 296: true},
  5: {474: true, 578: true, 1036: true, 391: true, 987: true, 459: true, 917: true, 296: true}
};

I need to

a) Locate the first key in my index based on my month value. E.g var month = 4;

b) Locate whether true or false for my ID value in that sub-object e.g var ID = 917

Some examples of what I need to check:

if month == 1 and ID == 459 return true

if month == 1 and ID == 917 return false (917 not present in data for month 1

if month == 4 and ID == 987 return true

if month == 5 and ID == 917 return true

if month == 5 and ID == 100 return false (100 not present in data for month 5)

I have safeguarded that all months 1-11 are present in the data, so I don't need to have an extra check to see if the month value exists in the object. It just needs to locate the first key using month and then search in that sub-object for the ID and if it finds it return true.

user1486133
  • 1,309
  • 3
  • 19
  • 37
  • 1
    `if (myData[month][ID]) { /* do something */ }` - No iteration or searching necessary, just test the properties directly. – nnnnnn Aug 17 '16 at 11:09
  • `" It just needs to locate the first key "` there can only ever be one key that is `month`, just to be precise here, if we are talking about learning. As far as the question, just access them and safeguard against `undefined` values and you are good to go. – Dellirium Aug 17 '16 at 11:19

4 Answers4

2

You can just do

!!myData[month][ID]

so that it will turn undefined into false, and true will stay true

dave
  • 62,300
  • 5
  • 72
  • 93
  • While this is the correct answer, if she was to be looking for something other then true/false this would not work, and since the start of the question is "I am trying to learn how objects work" I believe this is not the proper way of handling it. – Dellirium Aug 17 '16 at 11:19
2

You could use a check and cast the result to boolean.

function check(month, id) {
    return !!(myData[month] && myData[month][id]);
}

var myData = { 1: { 474: true, 459: true }, 4: { 474: true, 578: true, 987: true, 459: true, 917: true, 296: true }, 5: { 474: true, 578: true, 1036: true, 391: true, 987: true, 459: true, 917: true, 296: true } };

console.log(check(1, 459)); // true
console.log(check(1, 917)); // false (917 not present in data for month 1
console.log(check(4, 987)); // true
console.log(check(5, 917)); // true
console.log(check(5, 100)); // false (100 not present in data for month 5)
console.log(check(0, 100)); // false (no data present for month 0)
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • please explain why `!!` before return? is that because some missing `myData[month][id]` returns `undefined` so `!!undfined` means `false`? – A l w a y s S u n n y May 05 '18 at 02:51
  • 1
    @BeingSunny, right, OP want a result of boolean values and the first check returns `undefined` or the object and with the second check it returns `undefined` or `true`. for casting the result a double bang is the sortest way to convert a truthy/falsy value to `true` or `false`. – Nina Scholz May 05 '18 at 07:30
  • Awesome...thanks for the explanation. One more favour from you, can you suggests me some JS blogs or online web sites that will help me understand Clearly the JS things. I usually see MDN, Stackoverflow, Medium and daily js. Actually the way you answer complicated things of JS I am really fan of yours. – A l w a y s S u n n y May 05 '18 at 08:18
  • Thanks for the link, I like the detailed things coz it helps me to understand clearly. Thnx again.. – A l w a y s S u n n y May 05 '18 at 08:38
2

As you might already knew the { } syntax defines an object in Javascript.

Objects are always structured by key/value pair where the key has to be an unique value, while the value can be anything. E.g. another object, numbers.

To access an object's value by key you typically will do myData.5 this would return you the nested object {474: true, 578: true, 1036: true, 391: true, 987: true, 459: true, 917: true, 296: true}

But you only use the dot notation if you can preemptive what you want ahead, for instance the key 1 from the object. In certain scenario where you are not sure what keys you will be accessing. E.g. after some data processing and the algorithm could be accessing value from key 3 and sometimes from key 1 then for such cases, you do myData["5"]. Where "5" string value would normally be a variable. This is so that you can dynamically access the key based on the variable's value.

So for your case; if you are wanting to just check whether a key exists for a particular month then just do;

myData[month][ID] if this check returns true then we know that the month has that key. False otherwise.

var myData = {
    1: {474: true, 459: true},
    4: {474: true, 578: true, 987: true, 459: true, 917: true, 296: true},
    5: {474: true, 578: true, 1036: true, 391: true, 987: true, 459: true, 917: true, 296: true}
};

if (myData[1][474]) {
    console.log("key 474 exists in month 1.");
}
if (myData[1][974]) {
    console.log("key 974 exists in month 1.");
}
else {
    console.log("Key 974 doesn't exists for month 1.");
}
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
0

Input Data:

var myData = {
  1: {474: true, 459: true},
  4: {474: true, 578: true, 987: true, 459: true, 917: true, 296: true},
  5: {474: true, 578: true, 1036: true, 391: true, 987: true, 459: true, 917: true, 296: true}
};

a) Locate the first key in my index based on my month value. E.g var month = 4;

  • You would simply do this, myData["4"], this will return {474: true, 578: true, 987: true, 459: true, 917: true, 296: true}

  • Lets take this in a variable, like this var subObject = myData["4"];

b) Locate whether true or false for my ID value in that sub-object e.g var ID = 917

  • You can do the same thing again, subObject["917"], this will return true.

  • If subObject does not have the ID, then it will return undefined.

  • So possible values returned could be true, false or undefined.

  • Lets take this in a variable as well, var value = subObject["917"]. To always get boolean value, we can use double negation, like this !!value. This will make undefined, null, false and empty string to be returned as false while true stays true

In short, as Dave suggested, we can get rid of the variables and write it in one line as !!myData[month][ID].

However, please note that if myData[month] is undefined, then myData[month][ID] will throw an error so its always wise to check it in a if-condition. Check this question for more info Checking if a key exists in a JavaScript object?

if(("4" in myData) && ("917" in myData["4"])){
   return myData["4"]["917"];
}
Community
  • 1
  • 1
Flying Gambit
  • 1,238
  • 1
  • 15
  • 32