0

In the code below, priorityData array is in [80]="High" format i.e 80 is the integer value of High. I want to extract and use integer value 80 whenever my string is equivalent to "High". How should I do that?

if (service.priorityData[i] === priorityString) {
    logger.info("Priority string", service.priorityData[i].values());
    return service.priorityData[i].values();
} else {
    return null;
}

service.priorityData = {0 : "None", 20: "Low, 80: "High"}

But it doesn't return anything when I use this code.

12
  • 2
    You're not making sense. Is [80]="High" a string or are you saying that the 80th element in the array is equal to "High". Commented Apr 22, 2016 at 22:04
  • Sorry if the question is not clear. I mean priority data is in format [80] = "High". 80 is the integer value of string "High". and I want to use 80 instead of String "High" Commented Apr 22, 2016 at 22:09
  • 2
    Okay but where does 80 live? Is that the index of the array? Give me an example of service.priorityData[i]. Commented Apr 22, 2016 at 22:10
  • 1
    Then it sounds like it's a backwards dictionary. The only way to retrieve that data without any other reference point would be to do a loop over the keys of the dictionary and check priorityData[key] === "High" then key will be 80. Commented Apr 22, 2016 at 22:20
  • 1
    If your data is exactly as you've shown in your edit then I was right the first time. i will be the value. Which would be the exact same thing as going back through the keys of an object. Commented Apr 22, 2016 at 22:31

3 Answers 3

1

I would switch the keys on your priority Data, then you can just return the value of the key, or null

service.priorityData = {
  "None": 0,
  "Low": 20,
  "High": 80
}

return service.priorityData[priorityString] || null
Sign up to request clarification or add additional context in comments.

1 Comment

i am using rest api to get the data. so i cannot change the data format
0

Simply loop through the object and return the key when the value matches what you want.

for (var key in service.priorityData) {
    if (service.priorityData[key] == priorityString) {
        return key;
    }
    return null; // not found
}

Comments

0

It seems like you try to access object property. In this case you can access it thru next convention:

service.priorityData['80']

1 Comment

The second option would throw and Uncaught SyntaxError: Unexpected number. Only the first option works to access and object, however I'm not sure that that's what op is asking

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.