js知道数组中某条数据的某个属性的值,怎么得到这条数据或者这条数据的下标
vararr=[{"id":22,"name":张三,"age":18,"sex":女,.....},{"id":23,"name":张四,"age":17,"sex":...
var arr=[{"id":22,"name":张三,"age":18,"sex":女,.....},{"id":23,"name":张四,"age":17,"sex":男,.....},{"id":24,"name":王五,"age":18,"sex":男,.....},{"id":25,"name":李六,"age":18,"sex":女,.....},{"id":29,"name":赵七,"age":18,"sex":女,.....},.............]
我现在只知道id=24,怎么获得id=24的这条数据,或者id=24的这条数据的下标 展开
我现在只知道id=24,怎么获得id=24的这条数据,或者id=24的这条数据的下标 展开
2个回答
展开全部
var arr=[{"id":22,"name":'张三',"age":18,"sex":'女'},{"id":23,"name":'张四',"age":17,"sex":'男'},{"id":24,"name":'王五',"age":18,"sex":'男'},{"id":25,"name":'李六',"age":18,"sex":'女'}]
for (var value of arr) {
if(value.id == "24")
console.log(value);
}
for (var value of arr) {
if(value.id == "24")
console.log(value);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-01-19
展开全部
function getArrayItemIndex(arr, id) {
var index = -1;
if (typeof Array.prototype.findIndex === 'function') {
index = arr.findIndex(function(item) {
return item.id === id;
});
}
else {
for (var i = 0; i < arr.length;i++) {
if (arr[i].id === id) {
index = i;
break;
}
}
}
return index;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询