【问题标题】:how to loop through a JSON object with typescript Angular5 that returns a array of objects如何使用返回对象数组的打字稿Angular5遍历JSON对象
【发布时间】:2018-04-27 17:57:02
【问题描述】:

这是我的 json 代码,它返回一个数组“客户”,其中包含对象内的对象和数组

这是我的 json 鳕鱼:

{
  "Customers": [
    {
      "customerData": {
        "secondLastName": "Apale",
        "firstLastName": "Lara",
        "phoneNumber": "2711292033",
        "address": "Calle X avenida Y #100",
        "paymentCapacity": 18000,
        "gender": "Femenino",
        "name": "Yessica",
      },
      "orders": [
        {
          "amount": 34371,
          "term": "2017-07-21T17:32:28Z",
          "payment": 1423,
          "id": 12345678,
          "calculationDate": "2017-07-21T17:32:28Z",
          "products": [
            {
              "SKUNumber": 28005417,
              "quantity": 1,
              "SKULineDescription": "Computadoras",
              "SKUDescription": "Laptop HP",
              "SKULineId": 4
            }
          ]
        }
      ]
    },
    {
      "customerData": {
        "secondLastName": "González",
        "firstLastName": "Pineda",
        "phoneNumber": "55678420",
        "address": "Calle 26 #4732 Col. Pradera",
        "paymentCapacity": 180,
        "gender": "Femenino",
        "name": "María",
      },
      "orders": [
        {
          "amount": 34371,
          "term": "2017-07-21T17:32:28Z",
          "payment": 1423,
          "id": 12678422,
          "calculationDate": "2017-07-21T17:32:28Z",
          "products": [
            {
              "SKUNumber": 28005417,
              "quantity": 1,
              "SKULineDescription": "Computadoras",
              "SKUDescription": "Laptop HP",
              "SKULineId": 4
            }
          ]
        }
      ]
    }
  ]
}

这是我的数组的声明:arrCustomers = new Array(); 我尝试使用 foreach 遍历 json,但出现错误提示:未定义,这是我的控制台

【问题讨论】:

  • 请发布您的实际代码。
  • 分享您的 javascript 代码,而不仅仅是对象。
  • 我尝试打印这样的元素:console.log('Element: ' + this.objCustomers[0]);并像这样循环遍历 for(let key of Array.from( this.arrCustomers.keys()) ) { console.log(key); }
  • 你想做什么?深入列出对象的所有属性?只有包含数组的属性?无论如何,您的方法是Object.keysArray.reduce 和递归。解释清楚一点,以便我能提供帮助。
  • Quiero 加入了一个 un elemento específico "customerData" para obtener sus "pedidos" y "productos"。我是打字稿新手,

标签: javascript json angular


【解决方案1】:

我猜您在访问数组和对象的元素时遇到了问题。如果是这种情况,那么这可能对您有所帮助。

另外,运行那些被评论的sn-ps。

您可以通过两种方式访问​​对象属性,一种是使用点(。)运算符,另一种是在示例中使用。

var array = {
  "Customers": [
    {
      "customerData": {
        "secondLastName": "Apale",
        "firstLastName": "Lara",
        "phoneNumber": "2711292033",
        "address": "Calle X avenida Y #100",
        "paymentCapacity": 18000,
        "gender": "Femenino",
        "name": "Yessica",
      },
      "orders": [
        {
          "amount": 34371,
          "term": "2017-07-21T17:32:28Z",
          "payment": 1423,
          "id": 12345678,
          "calculationDate": "2017-07-21T17:32:28Z",
          "products": [
            {
              "SKUNumber": 28005417,
              "quantity": 1,
              "SKULineDescription": "Computadoras",
              "SKUDescription": "Laptop HP",
              "SKULineId": 4
            }
          ]
        }
      ]
    },
    {
      "customerData": {
        "secondLastName": "González",
        "firstLastName": "Pineda",
        "phoneNumber": "55678420",
        "address": "Calle 26 #4732 Col. Pradera",
        "paymentCapacity": 180,
        "gender": "Femenino",
        "name": "María",
      },
      "orders": [
        {
          "amount": 34371,
          "term": "2017-07-21T17:32:28Z",
          "payment": 1423,
          "id": 12678422,
          "calculationDate": "2017-07-21T17:32:28Z",
          "products": [
            {
              "SKUNumber": 28005417,
              "quantity": 1,
              "SKULineDescription": "Computadoras",
              "SKUDescription": "Laptop HP",
              "SKULineId": 4
            }
          ]
        }
      ]
    }
  ]
}

console.log("the whole array ==>",array);
//console.log("customers attribute ==>",array["Customers"]);
//console.log("first customerData ==>",array["Customers"][0]);
//console.log("first customerData's order ==>",array["Customers"][0]["orders"]);
//console.log("first customerData's all products ==>",array["Customers"][0]["orders"].map(i => i["products"]));
//console.log("All customerDatas", array["Customers"].map(i => i["customerData"]));
//console.log("All orders", array["Customers"].map(i => i["orders"]));
//console.log("All orders of all customers", array["Customers"].map(i => i["orders"]));

【讨论】:

  • 我想在一个数组中找到一个特定的对象,我已经声明了我的数组“arrCustomers = new Array ();”我有这样的方法: findOrdersByCustomer (dataCustomer: CustomerData) { console.log ('Element:' + this.arrCustomers [0]); // 在控制台中 this 未定义 this.arrCustomers.find (item => item.customerData === dataCustomer); // 我想按客户特定的顺序显示订单 }
  • @YessicaApaleLara, try, console.log ('Element:' , arrCustomers [0]); .我的意思是尝试不使用 this
  • 我的 arrCustomers 是一个全局属性
  • 你可以为你的问题创建一个 jsfiddle 吗?
猜你喜欢
  • 2021-05-24
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
相关资源
最近更新 更多