我有一个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
值的对象?
答案 0 :(得分:2)
您可以使用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())

答案 1 :(得分:1)
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;
}
}
}