how to get the key based on value in json using jquery -
how key based on value. of pass value 'a' should key 'attributes'
{ "employees": { "attributes": [ "a", "v", "c", "zz", "d", "e", "f", "g", "h", "i", "j" ], "both": [ "xxx", "yyy" ] } }
try utilizing object.keys
, array.prototype.filter
var data = { "employees": { "attributes": [ "a", "v", "c", "zz", "d", "e", "f", "g", "h", "i", "j" ], "both": [ "xxx", "yyy" ] } }; var q = "a"; var res = object.keys(data.employees).filter(function(val, key) { return data.employees[val].indexof(q) !== -1 })[0]; console.log(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Comments
Post a Comment