[英]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。
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.