Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 在JSON对象的数组中查找值_Javascript_Json - Fatal编程技术网

Javascript 在JSON对象的数组中查找值

Javascript 在JSON对象的数组中查找值,javascript,json,Javascript,Json,下面是JSP中JSON对象的数组 "Titles":[ { "Book3" : "BULLETIN 3" } , { "Book1" : "BULLETIN 1" } , { "Book2" : "BULLETIN 2" } ] 在JS端,它被解析,我看到一个包含3个对象的数组。 现在,我想在传递字符串键时查找/标识一个值 例如,当我通过“B

下面是JSP中JSON对象的数组

"Titles":[                          
    {
    "Book3" : "BULLETIN 3"
    }   
    ,
    {
    "Book1" : "BULLETIN 1"
    }
    ,
    {
    "Book2" : "BULLETIN 2"
    }    
]
在JS端,它被解析,我看到一个包含3个对象的数组。 现在,我想在传递字符串键时查找/标识一个值

例如,当我通过“Book2”时,我应该得到值“Bulletin2”。 有人能帮我确定方法吗?

试试这个

var数据={
“头衔”:[{
“第三册”:“公告3”
}, {
“第1册”:“公告1”
}, {
“第二册”:“公告二”
}]
};
函数getValueByKey(键,数据){
变量i,len=data.length;
对于(i=0;i日志(getValueByKey('Book2',data.Titles))让我们创建一个函数来获取数组中的对象,该函数包含两个参数:数组和要获取的属性的键:

function getObjectInArray(arr, key) {    
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].hasOwnProperty(key)) return arr[i][key];
    }
}
具有:

var x = [{
    "Book3" : "BULLETIN 3"
}, {
    "Book1" : "BULLETIN 1"
}, {
    "Book2" : "BULLETIN 2"
}];

您可以使用以下方法获取值:

x.filter(function(value) {
    return value.hasOwnProperty(key); // Get only elements, which have such a key
}).shift()[key]; // Get actual value of first element with such a key
注意,若对象并没有定义这样的键,它将抛出一个异常

此外,如果有更多对象具有这样的键,则只返回第一个。如果需要使用该键从对象获取所有值,可以执行以下操作:

x.filter(function(value) {
    return value.hasOwnProperty(key); // Get only elements, which have such a key
}).map(function(value) {
    return value[key]; // Extract the values only
});
这将为您提供一个仅包含适当值的数组

此外,如果您使用的是
jQuery
,则可以使用
grep
而不是
filter

jQuery.grep(x, function(value) {
    return value.hasOwnProperty(key);
}) /* and so on */;

要实现这一点,必须循环遍历数组元素的键,并测试数组中是否存在给定的键,如果存在,则获取其值:

var jsonTitles=[
{“Book3”:“BULLETIN 3”},
{“Book1”:“BULLETIN 1”},
{“书籍2”:“公告2”}
]
函数getValue(键,数组){
for(数组中的变量el){
if(数组[el].hasOwnProperty(键)){
返回数组[el][key];
}
}
}

警报(getValue(“Book1”,jsonTitles))对于Javascript中的这种数组/集合操作,我建议您使用库。对我来说,它提供的功能使一切变得简单得多。就你而言:

function find_value(array, key) {
    // find will run the provided function for every object in array
    var obj_found = _.find(array, function(obj) {
        // keys returns the keys inside an object
        // so if the key of currently examined object 
        // is what we are looking for, return the obj
        if (_.keys(obj)[0] === key) {
            return obj;
        }
   });
   // if an object with such key was found return its value
   if (obj_found) {
      return obj_found[key];
   } else {
      return null;
   }
}
是我建议的一把有效的小提琴

jQuery.grep(x, function(value) {
    return value.hasOwnProperty(key);
}) /* and so on */;
function find_value(array, key) {
    // find will run the provided function for every object in array
    var obj_found = _.find(array, function(obj) {
        // keys returns the keys inside an object
        // so if the key of currently examined object 
        // is what we are looking for, return the obj
        if (_.keys(obj)[0] === key) {
            return obj;
        }
   });
   // if an object with such key was found return its value
   if (obj_found) {
      return obj_found[key];
   } else {
      return null;
   }
}