简体   繁体   English

是否可以在不使用索引号的情况下访问json数组元素?

[英]Is it possible to access a json array element without using index number?

I have the following JSON: 我有以下JSON:

{
   "responseObject": {
   "name": "ObjectName",
   "fields": [
   {
     "fieldName": "refId",
     "value": "2170gga35511"
   },
   {
     "fieldName": "telNum",
     "value": "4541885881"
   }]}
}

I want to access "value" of the the array element with "fieldName": "telNum" without using index numbers, because I don't know everytime exactly at which place this telNum element will appear. 我想使用"fieldName": "telNum"访问数组元素的"value" "fieldName": "telNum"而不使用索引号,因为我不知道每次这个telNum元素出现的确切位置。

What I dream of is something like this: 我梦想的是这样的:

jsonVarName .responseObject.fields['fieldname'='telNum'].value jsonVarName .responseObject.fields ['fieldname'='telNum']。value

Is this even possible in JavaScript? 这在JavaScript中是否可行?

You can do it like this 你可以这样做

var k={
   "responseObject": {
   "name": "ObjectName",
   "fields": [
   {
     "fieldName": "refId",
     "value": "2170gga35511"
   },
   {
     "fieldName": "telNum",
     "value": "4541885881"
   }]
}};
value1=k.responseObject.fields.find(
function(i)
{return (i.fieldName=="telNum")}).value;
console.log(value1);

It's not possible.. Native JavaScript has nothing similar to XPATH like in xml to iterate through JSON. 这是不可能的..原生JavaScript没有类似XPath的类似于xml来迭代JSON。 You have to loop or use Array.prototype.find() as stated in comments. 您必须按照注释中的说明循环或使用Array.prototype.find() It's experimental and supported only Chrome 45+, Safari 7.1+, FF 25+. 它是实验性的,仅支持Chrome 45 +,Safari 7.1 +,FF 25+。 No IE. 没有IE。

Example can be found here 示例可以在这里找到

There is JSONPath that lets you write queries just like XPATH does for XML. JSONPath允许您像XPATH一样为XML编写查询。

$.store.book[*].author  the authors of all books in the store
$..author               all authors
$.store.*               all things in store, which are some books and a red bicycle.
$.store..price          the price of everything in the store.
$..book[2]              the third book
$..book[(@.length-1)] 
$..book[-1:]            the last book in order.
$..book[0,1]
$..book[:2]             the first two books
$..book[?(@.isbn)]      filter all books with isbn number
$..book[?(@.price<10)]  filter all books cheapier than 10
$..*                    All members of JSON structure.

It might be a better option to modify the array into object with fieldName as keys once to avoid using .find over and over again. 使用fieldName作为键将数组修改为对象可能是更好的选择,以避免.find遍地使用.find

fields = Object.assign({}, ...fields.map(field => {
    const newField = {};
    newField[field.fieldName] = field.value;
    return newField;
}

You will have to loop through and find it. 你将不得不循环并找到它。

 var json = { "responseObject": { "name": "ObjectName", "fields": [ { "fieldName": "refId", "value": "2170gga35511" }, { "fieldName": "telNum", "value": "4541885881" }] }; function getValueForFieldName(fieldName){ for(var i=0;i<json.fields.length;i++){ if(json.fields[i].fieldName == fieldName){ return json.fields[i].value; } } return false; } console.log(getValueForFieldName("telNum")); 

Clean and easy way to just loop through array. 只需循环遍历数组的简洁方法。

var json = {
   "responseObject": {
   "name": "ObjectName",
   "fields": [
    {
     "fieldName": "refId",
     "value": "2170gga35511"
    },
  {
    "fieldName": "telNum",
    "value": "4541885881"
  }]
 }   
  $(json.responseObject.fields).each(function (i, field) {
    if (field.fieldName === "telNum") {

      return field.value // break each
    }
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript / JSON-使用键名作为索引访问数组元素 - JavaScript / JSON - Access array element using a key name as index 如何在不使用索引的情况下访问JSON数组值 - How to access JSON array values without using index 如何访问没有索引的数组元素 ([]) - How to access array element without index ([]) 如何遍历数组,提供对元素,索引和数组本身的访问,而无需使用内置的Javascript函数 - How to iterate over arrays, providing access to the element, index, and array itself without using built in Javascript functions 是否可以在不使用 JSON 服务器的情况下使用 Javascript 访问 JSON 文件? - Is it possible to access a JSON file using Javascript without using a JSON server? 是否可以在不使用任何索引值和特定键的情况下从 json object 中的 json 数组中检索和打印数据 - is it possible to retrieve and print the data from a json object inside json array without using any index values and specific keys 使用数字作为“索引”(JSON) - Using number as "index" (JSON) 在索引处获取没有元素的数组 - Get array without element at index 通过使用JavaScript变量作为索引来访问PHP数组元素 - Access a PHP array element by using a JavaScript variable as index 在不使用内置indexOf函数的情况下查找数组元素的索引 - Find the index of an array element without using built in indexOf function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM