javascript - Typescript 迭代多维 json

标签 javascript json typescript

我有多维 JSON,需要迭代并适应为我的 View 格式化的 typescript 对象。

{
    "data": {
        "id": "71",
        "type": "quotations",
        "attributes": {
            "parts": {
                "name supplier 1": [
                    {
                        "id": 1,
                        "part_number": "10-09800",
                        "title": "FALCON ACCELEROMETER G METER -5G TO +10G",                            
                        "supplier_id": 1
                    },
                    {
                        "id": 3,
                        "part_number": "1999",
                        "title": "Peça teste",
                        "supplier_id": 1
                    }
                ],
                "name supplier 2": [
                    {
                        "id": 2,
                        "part_number": "10-09800",
                        "title": "FALCON ACCELEROMETER G METER -5G TO +10G",
                        "supplier_id": 2
                    }
                ]
            }
        }
    }
}

如何通过根据各自的 vendor 分隔项目来迭代此 JSON。

private responseToQuotationSuppliers(response: Response): QuotationSupplier {
    const collection = response.json().data.attributes['parts'];
    const suppliers: QuotationSupplierPart[] = [];
    const parts: Part[] = [];

    for (const i in collection) {
      for (const j in collection[i]) {
        collection[key].forEach(item => {
          let part = new Part(
            item.id,
            item.part_number,
            item.tile,
            item.supplier_id
          )
          parts.push(part);
        })
      }

      let supplier = new QuotationSupplierPart(
        key,
        parts
      )
      suppliers.push(supplier);
    }

    return new QuotationSupplier(
      suppliers,
      response.json().data.id
    )
  }

我就是这样做的,但所有 vendor 都得到了所有的东西。

有人能给我一个启发,让我以正确的方式迭代这个 JSON 吗?

最佳答案

private responseToQuotationSuppliers(response: Response): QuotationSupplier {
const collection = response.json().data.attributes['parts'];
const suppliers: QuotationSupplierPart[] = [];


for (const i in collection) {
            let parts: Part[] = [];
  for (const j in collection[i]) {
    collection[key].forEach(item => {
      let part = new Part(
        item.id,
        item.part_number,
        item.tile,
        item.supplier_id
      )
      parts.push(part);
    })
  }

  let supplier = new QuotationSupplierPart(
    key,
    parts
  )
  suppliers.push(supplier);
}

return new QuotationSupplier(
  suppliers,
  response.json().data.id
)

}

关于javascript - Typescript 迭代多维 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712316/

相关文章:

javascript - 隐藏后如何保留 bootstrap popover 的内容,或者如何真正隐藏它

java - JSONObject.append 到对象 - 结果是嵌套数组?

Angular Apollo 错误处理

Angular 5 无法读取未定义的属性 'remove'

javascript - 如何使用正确的名称将Kotlin函数导出到Javascript

javascript - jQuery 悬停在堆叠元素上(带有 'tipsy' )

java - 如何访问 LinkedHashMap 中嵌套对象的数据

angular - 将静态对象转换为 Observable 和 forkjoin

php - 我在 Chrome 中看不到带有 onmessage 方法的 HTML5 EventSource 事件

java - 相同的 build.gradle 文件获取不同版本的依赖项(org.json :json)