javascript - 从 4 级 json 对象数组返回特定​​ json 项

标签 javascript arrays 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: Cannot read property 'document' of undefined

还有这个(同样的错误):

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())

关于javascript - 从 4 级 json 对象数组返回特定​​ json 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50665850/

相关文章:

javascript - Ngrx 选择器 : createSelector VS pure rxjs

python beautiful-soap json - 抓取一页但不抓取其他类似的页面

ruby-on-rails - 使用关联数据发布 JSON

javascript - 如何从控制台隐藏脚本?

javascript - Bcrypt 比较密码时给出 false

java - 将特定索引处的数组值复制到另一个数组

javascript - 如何将 json 数组转换为 javascript 数组

Node.js 中的 JSON 解析错误

javascript - 比较 jquery timepicker 值

java - 在数组中搜索元素,然后返回带有索引的新数组