Question: Here i need get a value from JavaScript object, if the first 8 digits of property is matched with argument.

Here is what I'm tried...

```
var input = { 4546546512349: {}, 7200000000007: {}, 9399543200000: {} }
function find_key(query){
  $.each(input, function(k, v) {
    if (k.substring(0,8) == query){
      console.log(k);
      return k        
    }  
  });
}
find_key(45465465);

```

Is there any best solution for this. Thanks in advance.

Answer: 
A solution with [`Array#filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). It returns an array with the matched keys.

```js
function findKey(query) {
    return Object.keys(input).filter(function (k) {
        return k.substring(0, 8) == query;
    });
}

var input = { 4546546512349: {}, 7200000000007: {}, 9399543200000: {} }
document.write('<pre>' + JSON.stringify(findKey(45465465), 0, 4) + '</pre>');
```
Your code already works, but if you are looking for a more concise solution then

```
function find_key(input,query)
{ 
   return input[Object.keys(input).filter( function(value){ if (value.indexOf( "45465465" ) == 0) {return value;}})[0]];
} 
var input = { 4546546512349: {}, 7200000000007: {}, 9399543200000: {} };
find_key(input,45465465);

```

One fundamental change done

* **Input** being passed as argument to make it more re-usable