从4级json对象的数组中返回特定的json项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从4级json对象的数组中返回特定的json项相关的知识,希望对你有一定的参考价值。
我有一个json对象数组。第一个对象始终具有空数据,第二个和其他项始终具有相同的结构。
如何返回包含特定document
字段(x.data.outputs.document
)的一个数组项(1个对象)?
var x = [
timestamp: 3455435654345,
hash: "f78ed219d5b60a3665e7382f",
data: [],
nonce: 0,
difficulty: 2
,
timestamp: 1528020475945,
hash: "01a3c43290652bc8f858651dc5",
data: [
id: "fc453bd0-6715-11e8-b7d9-1156bded578e",
input:
timestamp: 1528020475917,
address:
"0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4dcQ3P5fs",
signature:
"af43a84e5e0e59b9af713cc5e99ce768b318e5"
,
outputs: [
document: "a8ab1940bf8a7e13fe8e805470756a28",
address:
"IGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC"
]
],
nonce: 2,
difficulty: 1
,
timestamp: 1528020491868,
hash: "fb47a96d8a3bce4d81fe88122d60266a",
data: [
id: "05c5ca30-6716-11e8-b7d9-1156bded578e",
input:
timestamp: 1528020491859,
address:
"A0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4dcQ3P5",
signature:
"152d22b6328bea1c4815bad8d639248bb11a"
,
outputs: [
document: "5cbe7e76bc24d54e71bcb45daa793a3c",
address:
"Q3P5fsOZMemlIRiXx5Qfo7\nGCaa7eNu82Cl"
]
],
nonce: 1,
difficulty: 2
];
我试过这个:
x.find(
item =>
item.data.outputs.document == "a8ab1940bf8a7e13fe8e805470756a28"
)
TypeError:无法读取未定义的属性“文档”
而这(同样的错误):
var newArray = str.filter(function(el)
return el.data.outputs.document == "5cbe7e76bc24d54e71bcb45daa793a3c";
);
是否有可能访问document
字段并找到具有一些document
值的对象?
答案
您可以使用obj && obj.prop1 && obj.prop1.prop2
等语法来保护对嵌套属性的访问。
var data = [
timestamp: 3455435654345,
hash: "f78ed219d5b60a3665e7382f",
data: [],
nonce: 0,
difficulty: 2
,
timestamp: 1528020475945,
hash: "01a3c43290652bc8f858651dc5",
data: [
id: "fc453bd0-6715-11e8-b7d9-1156bded578e",
input:
timestamp: 1528020475917,
address: "0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4dcQ3P5fs",
signature: "af43a84e5e0e59b9af713cc5e99ce768b318e5"
,
outputs: [
document: "a8ab1940bf8a7e13fe8e805470756a28",
address: "IGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC"
]
],
nonce: 2,
difficulty: 1
,
timestamp: 1528020491868,
hash: "fb47a96d8a3bce4d81fe88122d60266a",
data: [
id: "05c5ca30-6716-11e8-b7d9-1156bded578e",
input:
timestamp: 1528020491859,
address: "A0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4dcQ3P5",
signature: "152d22b6328bea1c4815bad8d639248bb11a"
,
outputs: [
document: "5cbe7e76bc24d54e71bcb45daa793a3c",
address: "Q3P5fsOZMemlIRiXx5Qfo7\nGCaa7eNu82Cl"
]
],
nonce: 1,
difficulty: 2
];
function findObjectsWithAddress(address)
return data.filter(
a =>
a.data &&
a.data.find(
b =>
b.outputs &&
b.outputs.find(c => c.address == address)
)
)
function extractAddresses()
var addresses = []
for (let a of data)
if (!a.data)
continue;
for (let b of a.data)
if (!b.outputs)
continue;
for (let c of b.outputs)
if ('address' in c)
addresses.push(c.address)
return addresses
console.log(findObjectsWithAddress('IGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC'))
console.log(extractAddresses())
另一答案
data
和outputs
是数组,您需要在访问其子代之前使用适当的索引访问它,例如:el.data[0].outputs[0].document
。在您的情况下,您正在搜索特定值,因此它应该是:
var x = [
timestamp: 3455435654345,
hash: "f78ed219d5b60a3665e7382f",
data: [],
nonce: 0,
difficulty: 2
,
timestamp: 1528020475945,
hash: "01a3c43290652bc8f858651dc5",
data: [
id: "fc453bd0-6715-11e8-b7d9-1156bded578e",
input:
timestamp: 1528020475917,
address:
"0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4dcQ3P5fs",
signature:
"af43a84e5e0e59b9af713cc5e99ce768b318e5"
,
outputs: [
document: "a8ab1940bf8a7e13fe8e805470756a28",
address:
"IGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC"
]
],
nonce: 2,
difficulty: 1
,
timestamp: 1528020491868,
hash: "fb47a96d8a3bce4d81fe88122d60266a",
data: [
id: "05c5ca30-6716-11e8-b7d9-1156bded578e",
input:
timestamp: 1528020491859,
address:
"A0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4dcQ3P5",
signature:
"152d22b6328bea1c4815bad8d639248bb11a"
,
outputs: [
document: "5cbe7e76bc24d54e71bcb45daa793a3c",
address:
"Q3P5fsOZMemlIRiXx5Qfo7\nGCaa7eNu82Cl"
]
],
nonce: 1,
difficulty: 2
];
outer: for(let item of x)
for(let data of item.data)
let match = data.outputs.find(o => o.document === "a8ab1940bf8a7e13fe8e805470756a28");
if(match)
console.log(match);
break outer;
以上是关于从4级json对象的数组中返回特定的json项的主要内容,如果未能解决你的问题,请参考以下文章
如何通过使用 node.js 从实时 json 数据中提取特定字段来创建新的单个 json 对象