Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取对象';数组中的值是多少?_Javascript_Jquery - Fatal编程技术网

Javascript 如何获取对象';数组中的值是多少?

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

我有几个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-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的“高度”
;)也许你可以把它添加到你的代码中。对不起我把这个东西弄丢了