1

I have the following switch/case statement which returns data from the json object below. In reality this will be very long!

Switch statement

switch(val){
    case 'chicken':
        return json['meat']['main'][val]; 
        break;
    case 'beef':
        return json['meat']['main'][val]; 
        break
    case 'lamb':
        return json['meat']['main'][val]; 
        break;
    case 'pork':
        return json['meat']['main'][val]; 
        break;
    default:
        return '';
}

json object

json={
  "meat": {
    "main": {
      "chicken": "Roast chicken and vegetables",
      "beef": "Beef and Yorkshire pudding",
      "lamb": "Lamb shank with red currant gravy",
      "pork": "Pork and apple sauce",
    }
  }
}

As you can see, the switch statement is very repetitive as every case statement returns the same expression (although the value it returns will be different as it is accessing a variable key [val] in the object).

In reality my json file is huge, so I want to avoid manually typing out a case statement for each meat (chicken, beef, lamb, pork). Instead I would like to iterate through my json object getting the value at json['meat']['main'][i] (where i is a counter) to create each case. Is this possible? And if not, is there an alternative approach?

Many thanks!

Katie

2
  • can you explain a bit more of your use case? For the above case you might not need a switch statement. return json.meat.main[val] should work Commented Apr 9, 2020 at 15:47
  • Your switch statement doesn't make any sense. If you use json['meat']['main'][val], you are directly accessing correct value. Commented Apr 9, 2020 at 15:48

1 Answer 1

2

Is this what you need?

let json = {
  "meat": {
    "main": {
      "chicken": "Roast chicken and vegetables",
      "beef": "Beef and Yorkshire pudding",
      "lamb": "Lamb shank with red currant gravy",
      "pork": "Pork and apple sauce",
    }
  }
}


const search = (obj, val) => {      
  const keys = Object.keys(obj)
  for (const key of keys) {
    if (typeof obj[key] === 'string') {
      if (key === val) {
        return obj[key]
      }
    } else {
      const description = search(obj[key], val)
      if (description) {
        return description
      }
    }
  }
  return ''
}


const description = search(json, 'beef')
console.log(description)
Sign up to request clarification or add additional context in comments.

4 Comments

isn't it something like this? return json.meat.main['chicken']
I'm assuming val could be something like "apple", which wont fall under the "meat" category. So you can't do json.meat.main['apple'] or json.fruit.main['apple'].
Yes. it makes sense if that is the case. but from the question he needs a simple return of meat.main['val']. Instead I would like to iterate through my json object getting the value at json['meat']['main'][i]
@twharmon - you have nailed it! That's so clever - loving your answer and it works perfectly when I put it into my real code, which is much more complex. Thank you so much! And you have interpreted my question exactly as I intended. You're right that I need everything to be variable, so would need to work for apple and other things etc. Thanks again - really happy with this solution :-)

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.