0

Assume I have a JSON object with elements like this:

{"number":"21344"}

and I want to find this exact element through an input form, that allows whitespaces (e.g. "213 44"). How do allow this pattern?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Thorra
  • 23
  • 5
  • it's a json array or a json object ? I mean it's this `[{"number":"21344"}, {"number":"23423"}, {"number":"75656"}]` or this `{{"number":"21344"}, {"number":"23423"}, {"number":"75656"}}`? – Nelson Teixeira Feb 17 '17 at 17:41
  • @NelsonTeixeira The first one. However, I found the solution in my case is simply to edit the number variable to remove whitespaces before it is read! http://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript – Thorra Feb 17 '17 at 17:45

1 Answers1

0

You can remove all whitespace from a string with:

str = str.replace(/\s/g, '');

In your posted JSON you are apparently trying to search for JSON keys ("number") based on their value ("21344"). If you really want to do that, you could do the following:

var keys = Object.keys(yourJsonObject);
for (var i = 0; i < keys.length; i++) {
    if (yourJsonObject[keys[i]] === "21344") {
        return keys[i];
    }
}
Juuso
  • 487
  • 5
  • 12