How do we proceed with the search key to the object?



  • There's a function that's looking for. value in the facility. How can you make it look on the key?

    For example, this is how it works:

    when function search('r', items) argument rm will give words where there is r

    // ["bar", "lorem", "dolor"]

    It'd be nice if you could find the keys. For example:

    If the argument is foomust issue: // ["bar", "lorem", "dolor"]

    a if the argument bar It should be: ["amet","ipsum","dolor"]

    whole function:

    function search(s, arr) {
      var matches = [];
    

    for (var i = arr.length; i--;) {
    for (key in arr[i]) {
    if (arr[i].hasOwnProperty(key) && arr[i][key].indexOf(s) > -1)
    matches.push(arr[i][key]);
    }
    }
    return matches;
    };

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

    search('r', items); // ["bar", "lorem", "dolor"]



  • Similar:

    function search(s, arr) {
      var matches = [];
    

    for (var i = arr.length; i--;) {
    for (key in arr[i]) {
    if (arr[i].hasOwnProperty(key) && key.indexOf(s) > -1) {
    matches.push(arr[i][key]);
    }
    }
    }
    return matches;
    };

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

    search('foo', items);




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2