0

I have an object acting like Hash table.

var ob = {
  "Earnings": {
    "name": "Finance",
    "id": "0"
  },

  "Total": {
    "token_id": 90,
    "Token": {
      "name": "E32"
    }
  }
}

Now the point is that I have value not key of ob.

I have value :

{
  "token_id": 90,
  "Token": {
    "name": "E32"
  }
}
}

I need to find whether the value exists in ob efficiently. I had tried the loop approach like this.

for (var key in ob) {
  if (ob.HasOwnProperty(key)) {
    //check for the value
  }
}

This approach is fine for small data but for large data its taking time. So, Is there any way I can solve this use case by getting(searching) in O(1) time?

Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
Akash Sateesh
  • 271
  • 2
  • 4
  • 13

1 Answers1

1

All I can think is to the convert the value in the json as a Set object

JavaScript implementation of a set data structure

when you query the set it will be O(1) amortized when you convert it to a set it will be O(n).

or you can swap the value and key and put in a HashMap which some people implement for javascript.

Hamuel
  • 633
  • 5
  • 16