user2128
user2128

Reputation: 640

Return array int value of the respective String

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.

Upvotes: 0

Views: 69

Answers (3)

Barmar
Barmar

Reputation: 781834

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
}

Upvotes: 0

Leonid Lazaryev
Leonid Lazaryev

Reputation: 326

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

service.priorityData['80']

Upvotes: 0

Scott Schwalbe
Scott Schwalbe

Reputation: 430

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

Upvotes: 1

Related Questions