ret*_*.at 51 javascript jquery
鉴于:
var peoples = [
{ "attr1": "bob", "attr2": "pizza" },
{ "attr1": "john", "attr2": "sushi" },
{ "attr1": "larry", "attr2": "hummus" }
];
Run Code Online (Sandbox Code Playgroud)
通缉:
对象索引attr === value例如attr1 === "john"或attr2 === "hummus"
更新: 请仔细阅读我的问题,我不想通过$ .inArray找到对象,也不想获取特定对象属性的值.请考虑这个以获得答案.谢谢!
jcu*_*nod 104
这些天所有很酷的孩子都在进行函数式编程(你好React用户)所以我想我会给出功能性解决方案.在我看来,它实际上比迄今为止提出的impratival for和each循环更好,并且使用ES6语法它非常优雅.
现在有一种很好的方法可以执行此操作findIndex,该方法使用一个函数返回true/ false基于数组元素是否匹配(尽管如此,检查浏览器兼容性).
var index = peoples.findIndex(function(person) {
return person.attr1 == "john"
}
Run Code Online (Sandbox Code Playgroud)
使用ES6语法,您可以编写以下代码:
var index = peoples.findIndex(p => p.attr1 == "john")
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找index地方peoples[index].attr1 == "john"使用:
var index = peoples.map(function(o) { return o.attr1; }).indexOf("john");
Run Code Online (Sandbox Code Playgroud)
步骤1
使用.map()得到赋予了特定键值的数组:
var values = object_array.map(function(o) { return o.your_key; });
Run Code Online (Sandbox Code Playgroud)
上面的这条线带你从这里:
var peoples = [
{ "attr1": "bob", "attr2": "pizza" },
{ "attr1": "john", "attr2": "sushi" },
{ "attr1": "larry", "attr2": "hummus" }
];
Run Code Online (Sandbox Code Playgroud)
到这里:
var values = [ "bob", "john", "larry" ];
Run Code Online (Sandbox Code Playgroud)
第2步
现在我们只是.indexOf()用来找到我们想要的键的索引(当然,这也是我们正在寻找的对象的索引):
var index = values.indexOf(your_value);
Run Code Online (Sandbox Code Playgroud)
解
我们结合以上所有内容:
var index = peoples.map(function(o) { return o.attr1; }).indexOf("john");
Run Code Online (Sandbox Code Playgroud)
或者,如果您更喜欢ES6语法:
var index = peoples.map((o) => o.attr1).indexOf("john");
Run Code Online (Sandbox Code Playgroud)
var peoples = [
{ "attr1": "bob", "attr2": "pizza" },
{ "attr1": "john", "attr2": "sushi" },
{ "attr1": "larry", "attr2": "hummus" }
];
var index = peoples.map(function(o) { return o.attr1; }).indexOf("john");
console.log("index of 'john': " + index);
var index = peoples.map((o) => o.attr1).indexOf("larry");
console.log("index of 'larry': " + index);
var index = peoples.map(function(o) { return o.attr1; }).indexOf("fred");
console.log("index of 'fred': " + index);
var index = peoples.map((o) => o.attr2).indexOf("pizza");
console.log("index of 'pizza' in 'attr2': " + index);Run Code Online (Sandbox Code Playgroud)
ret*_*.at 19
如果要在不干扰原型的情况下检查对象本身,请使用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;
}
Run Code Online (Sandbox Code Playgroud)
还包括原型属性,使用:
var getIndexIfObjWithAttr = function(array, attr, value) {
for(var i = 0; i < array.length; i++) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
使用jQuery .each()
var peoples = [
{ "attr1": "bob", "attr2": "pizza" },
{ "attr1": "john", "attr2": "sushi" },
{ "attr1": "larry", "attr2": "hummus" }
];
$.each(peoples, function(index, obj) {
$.each(obj, function(attr, value) {
console.log( attr + ' == ' + value );
});
});Run Code Online (Sandbox Code Playgroud)
使用for循环:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)
不是对你的问题的直接答案,尽管我认为值得一提,因为你的问题似乎适合“在键值存储中按名称获取事物”的一般情况。
如果你对“peoples”的实现方式不太了解,那么一种更类似于 JavaScript 的方式来找到合适的人可能是:
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;
Run Code Online (Sandbox Code Playgroud)
希望如果这不是您想要的答案,它可能会帮助对“键/值”问题感兴趣的人。