0

Sorry if it sounds like a stupid question

I have this following JavaScript object:

data = {
    "Max" : 100
}

This same object can also have the following form:

data = {
    "January": {"Max" : 100}
}

I want to use one same function to retrieve the value of Max if given the two forms

And it made me wonder if it is possible to write a conditional expressions directly in the [] when you write the values you want to retrieve? Is this following expression allowed in JavaScript? Something like this:

data[monthly ? 'month' : '']

Of course I tried it and it doesn't work. But is there another way to do such a thing in a line? monthly is a boolean

2 Answers 2

2

You can use the following script to do that, I have added some comments to make it clear

var data1 = {
  "Max" : 100
}

var data2 = {
  "January": {"Max" : 100}
}

// storing the months in an array to loop over and see if the current key is a month
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function getMax(data) {
  // if the key `Max` exists in data then just get the value of `data.Max` else loop over the months, and see if one of the months is a key and stores a truthy value, in this case it stores an object, so that's what we want and then get it's .Max value
  return "Max" in data ? data.Max : data[months.filter(m => data[m] ? m : "")[0]].Max;
}

console.log(getMax(data1));
console.log(getMax(data2));

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

4 Comments

excellent use of the in operator, a lot of people would probably go with just months.Max but this can behave weirdly if you have falsy values
@Zer0 Thank you for your feedback, I like to always write good code
This is good, But i want to get specific month and not check every month object to see if there's a "truthy" value
Pls update your question, my answer is about the current question, if you want me to update it, you need to update your question, and pls give the expected input and output as well
0

You can make use of Object.values

let data = {
  "Max": 100
};

const getMax = (data) => {
  //check if "Max" is available in the `data`, if so return `data.Max` else 
  //get values of the keys in the object using `Object.values` and 
  //retreive `Max` from the array
  return data.Max ? data.Max : Object.values(data)[0].Max
}

console.log(getMax(data));

data = {
  "January": {
    "Max": 200
  }
}

console.log(getMax(data));

There's yet another way of achieving this using Array.find and Optional Chaining.

let data = {
  Max: 100,
};

const getMax = (data = {}) => {
  return data.Max ? data.Max : Object.values(data).find(({ Max }) => Max)?.Max;
};

console.log(getMax(data));

data = {
  January: { Max: 200 },
};

console.log(getMax(data));

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.