0

I want to use js search in object, but with some feature. I clearly run this answer.

JS search in object values

[
  {
    "foo" : "bar",
    "bar" : "sit"
  },
  {
    "foo" : "lorem",
    "bar" : "ipsum"
  },
  {
    "foo" : "dolor",
    "bar" : "amet"
  }
]

This is fine, but I want to search sıt or sit and result will below.

[
  {
    "foo" : "bar",
    "bar" : "sit"
  }
]

How can I do this?

Community
  • 1
  • 1
abalta
  • 1,204
  • 1
  • 12
  • 21

1 Answers1

0

If you use the search() method instead of indexOf(), then you can use regex to do this:

var objects = [{
    "foo": "bar",
        "bar": "sit"
}, {
    "foo": "lorem",
        "bar": "ipsum"
}, {
    "foo": "dolor",
        "bar": "amet"
}];

var results = [];

var toSearch = /s(i|ı)t/;

for (var i = 0; i < objects.length; i++) {
    for (var key in objects[i]) {
        if (objects[i][key].search(toSearch) != -1) {
            results.push(objects[i]);
        }
    }
}

The result is all objects containing your string to match. Is this along the lines of what you're looking for?

Kay Cee
  • 321
  • 1
  • 2
  • 8
  • "sit" is just an example, I want to i|ı, ü|u, o|ö, ş|s and ğ|g. How can I add a regex expression and how to functionalite this? – abalta Jan 16 '15 at 13:54