如何捕獲嵌套對像中的元素?

爵士

我有一個數組transactionsArray的元素是對象的集合。對象依次具有嵌套元素。如何捕獲對像中的嵌套元素,特別是稱為 的元素的值transPayersNumber

在我的數組結構下面找到

transactions[
    {numberOfMobileTransactions: 200, day: Wednesday,
     phoneNumbers: [{ _id: 60bdf6c18fd22aac8c4b35f2,
                        transPayersNumber: '0705667966',
                        transCounty: 'Kiambo'
                     }] 
     },

    {numberOfMobileTransactions: 120,day: Tuesday, 
     phoneNumbers: [{ _id: 60bdf6c1654622aac8c4b35f2,
                        transPayersNumber: '0747897933',
                        transCounty: 'Kiambo'
                     },
                     { _id: 60bdf6sdtd4622aac8c4b35f2,
                        transPayersNumber: '0747845631',
                        transCounty: 'Nairobi'
                     }] 
     }
]

如何訪問嵌套Object 中 元素的值transactions.phoneNumbers.transPayersNumber

在嘗試訪問transPayersNumber元素值的失敗嘗試中,我嘗試了各種代碼,但下面的代碼是我所得到的,沒有任何錯誤消息:

transactions.forEach(transactions => {
    console.log("Transactions: " +transactions.day + " by: " +transactions.phoneNumbers )
});

上面的代碼產生:

Transactions: Wednesday by: {
  _id: 60bdf6c18fd22aac8c4b35f2,
  transPayersNumber: '0705667966',
  transCounty: 'Kiambo'
},
Transactions: Tuesday by: {
  _id: 60bdf6c18fd22aac8c4b35f2,
  transPayersNumber: '0705667966',
  transCounty: 'Kiambo'
},
Transactions: Tuesday by: {
  _id: 60bdf6c18fd22aac8c4b35f2,
  transPayersNumber: '0705667966',
  transCounty: 'Kiambo'
}

我想要的輸出類似於以下內容:

Transactions: Wednesday by: 0705667966
Transactions: Tuesday by: 0747897933, 0747845631
Srikar Phani Kumar Marti

這應該有效:

const transactions = [
  {
    "numberOfMobileTransactions": 200,
    "day": "Wednesday",
    "phoneNumbers": [
      {
        "_id": "60 bdf6c18fd22aac8c4b35f2",
        "transPayersNumber": "0705667966",
        "transCounty": "Kiambo"
      }
    ]
  },
  {
    "numberOfMobileTransactions": 120,
    "day": "Tuesday",
    "phoneNumbers": [
      {
        "_id": "60 bdf6c1654622aac8c4b35f2",
        "transPayersNumber": "0747897933",
        "transCounty": "Kiambo"
      },
      {
        "_id": "60 bdf6sdtd4622aac8c4b35f2",
        "transPayersNumber": "0747845631",
        "transCounty": "Nairobi"
      }
    ]
  }
];


const transPayersNumbersArray = [];

transactions.map(eachObj => {

  const phoneNumbers = eachObj.phoneNumbers;
  
  if (phoneNumbers && phoneNumbers.length) {
   phoneNumbers.map(eachPhoneNumberObj => {
      transPayersNumbersArray.push(eachPhoneNumberObj.transPayersNumber);
   })
  }

});

console.log('TransPayersNumbersArray ==>', transPayersNumbersArray)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何從對像中獲取元素?

如何在嵌套的對像數組中獲取對象路徑?

如何在要解析的嵌套對像數組中獲取 RxJs Observable 的值?

如何從 MongoDB 的嵌套對像中獲取特定詳細信息

如何按值過濾嵌套的對像數組並獲取根對象

如何在 MongoDB 中只獲取數組元素而不是整個對像作為輸出?

如何在android包導入問題中捕獲圖像?

對 Java 中的 lambda 捕獲感到好奇

如何按名稱從嵌套對像中刪除嵌套對象

如何在 vue 中捕獲非輸入元素上的 Enter 事件?

通過對像數組中的鍵獲取元素

如何在 Javascript 中更新嵌套對像數組中的鍵/值對

在 Bash 中,如何從嵌套在復雜語句中的命令中捕獲退出狀態代碼

如何在javascript中將嵌套對象轉換為對像數組?

反應如何在嵌套數據對像中呈現動態圖像

如何在對像數組中獲取具有唯一值的鍵?(JavaScript)

如何從對像數組中獲取特定鍵值的總和

如何從未指定的對像中獲取屬性?

未捕獲的錯誤:不能使用 stdClass 類型的對像作為數組

未捕獲的錯誤:當不在對像上下文中時使用 $this ......請幫助

如何在顫振中返回捕獲異常

如何從 HttpResponse java 對像類獲取?

捕獲 png/jpg 圖像中 html div 的內容

如何在 React 中“展平”嵌套對像數組?

如何從 React 中的嵌套對像數組中提取數據?

如何更改嵌套對像數組中的所有鍵名javascript

如何在MongoDB聚合管道中將對像數組轉換為嵌套對象

如何在javascript中按日期範圍從對像數組中獲取唯一數據

如何在對像數組中搜索一個值並在 Laravel 中獲取它?