Given:
var peoples = [
{ "attr1": "bob", "attr2": "pizza" },
{ "attr1": "john", "attr2": "sushi" },
{ "attr1": "larry", "attr2": "hummus" }
];
Wanted:
Index of object where attr === value
for example attr1 === "john"
or attr2 === "hummus"
Update: Please, read my question carefully, i do not want to find the object via $.inArray nor i want to get the value of a specific object attribute. Please consider this for your answers. Thanks!
This question is related to
javascript
jquery
Using jQuery .each()
var peoples = [_x000D_
{ "attr1": "bob", "attr2": "pizza" },_x000D_
{ "attr1": "john", "attr2": "sushi" },_x000D_
{ "attr1": "larry", "attr2": "hummus" }_x000D_
];_x000D_
_x000D_
$.each(peoples, function(index, obj) {_x000D_
$.each(obj, function(attr, value) {_x000D_
console.log( attr + ' == ' + value );_x000D_
});_x000D_
});
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
_x000D_
Using for-loop:
var peoples = [_x000D_
{ "attr1": "bob", "attr2": "pizza" },_x000D_
{ "attr1": "john", "attr2": "sushi" },_x000D_
{ "attr1": "larry", "attr2": "hummus" }_x000D_
];_x000D_
_x000D_
for (var i = 0; i < peoples.length; i++) {_x000D_
for (var key in peoples[i]) {_x000D_
console.log(key + ' == ' + peoples[i][key]);_x000D_
}_x000D_
}
_x000D_
If you want to check on the object itself without interfering with the prototype, use hasOwnProperty()
:
var getIndexIfObjWithOwnAttr = function(array, attr, value) {
for(var i = 0; i < array.length; i++) {
if(array[i].hasOwnProperty(attr) && array[i][attr] === value) {
return i;
}
}
return -1;
}
to also include prototype attributes, use:
var getIndexIfObjWithAttr = function(array, attr, value) {
for(var i = 0; i < array.length; i++) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}
function getIndexByAttribute(list, attr, val){
var result = null;
$.each(list, function(index, item){
if(item[attr].toString() == val.toString()){
result = index;
return false; // breaks the $.each() loop
}
});
return result;
}
You can also make it a reusable method by expending JavaScript:
Array.prototype.findIndexBy = function(key, value) {
return this.findIndex(item => item[key] === value)
}
const peoples = [{name: 'john'}]
const cats = [{id: 1, name: 'kitty'}]
peoples.findIndexBy('name', 'john')
cats.findIndexBy('id', 1)
Not a direct answer to your question, though I thing it's worth mentioning it, because your question seems like fitting in the general case of "getting things by name in a key-value storage".
If you are not tight to the way "peoples" is implemented, a more JavaScript-ish way of getting the right guy might be :
var peoples = {
"bob": { "dinner": "pizza" },
"john": { "dinner": "sushi" },
"larry" { "dinner": "hummus" }
};
// If people is implemented this way, then
// you can get values from their name, like :
var theGuy = peoples["john"];
// You can event get directly to the values
var thatGuysPrefferedDinner = peoples["john"].dinner;
Hope if this is not the answer you wanted, it might help people interested in that "key/value" question.
Do this way:-
var peoples = [
{ "name": "bob", "dinner": "pizza" },
{ "name": "john", "dinner": "sushi" },
{ "name": "larry", "dinner": "hummus" }
];
$.each(peoples, function(i, val) {
$.each(val, function(key, name) {
if (name === "john")
alert(key + " : " + name);
});
});
name : john
Refer LIVE DEMO
?
Source: Stackoverflow.com