Javascript 如何获取对象';数组中的值是多少?
我有几个id和高度的数组 我怎样才能得到一个特定id的高度 比如从数组中获取review-1的值?哪个是“500px” 谢谢Javascript 如何获取对象';数组中的值是多少?,javascript,jquery,Javascript,Jquery,我有几个id和高度的数组 我怎样才能得到一个特定id的高度 比如从数组中获取review-1的值?哪个是“500px” 谢谢 ar=[]; ar.push({"id":"reiew-1","height":"500px"}); $.each(ar,function(index,value){ alert(value.height); // gets all the heights }); 在循环中使用if条件 ar = []; ar.push({ "id": "reiew
ar=[];
ar.push({"id":"reiew-1","height":"500px"});
$.each(ar,function(index,value){
alert(value.height); // gets all the heights
});
在循环中使用if条件
ar = [];
ar.push({
"id": "reiew-1",
"height": "500px"
});
$.each(ar, function (index, value) {
if (value.id == 'reiew-1') {
alert(value.height); // gets all the heights
return false;//stop further looping of the array since the value you are looking for is found
}
});
因此,您只能使用javascript方法来完成这项工作
var ar=[];
ar.push({"id":"reiew-1","height":"500px"}, {"id":"reiew-3","height":"500px"});
// function that filter and return object with selected id
function getById(array, id){
return array.filter(function(item){
return item.id == id;
})[0].height || null;
}
// now you can use this method
console.log(getById(ar, "reiew-1"))
您可以使用此代码,您可以使用
功能性
执行以下操作:
ar=[
{"id":"reiew-1","height":"500px"},
{"id":"reiew-2","height":"600px"},
{"id":"reiew-3","height":"700px"},
];
filterById=function(value){
return function(o){
return o["id"]===value;
};
}
getAttribute=function(value){
return function(o){
return o[value];
}
}
ar.filter(filterById("reiew-1")).map(getAttribute("height"))
这对眼睛来说很容易:]
这是你的电话号码
有关更多信息(例如关于浏览器兼容性),请参见右侧的MDN链接和。但是他想要一个特定的
id的“高度”
;)也许你可以把它添加到你的代码中。对不起我把这个东西弄丢了