How to extract a property from array of objects and slice it?

后端 未结 2 1030
故里飘歌
故里飘歌 2021-01-28 01:21

so i have an array of objects which returns phoneNumber and businessNumber and name. I am trying to extract the businessNumber and phoneNumber and want to slice them from \"-\"

相关标签:
2条回答
  • 2021-01-28 02:10

    you could return both numbers in an array of new objects

    let arr1 = myArray.map(function(obj) {
      return {
        businessNumber: obj.businessNumber.split('-').pop(),
        phoneNumber: obj.phoneNumber.split('-').pop() 
      }
    })
    
    0 讨论(0)
  • 2021-01-28 02:23

    You could do something like this if you change your map function slightly.

    myArray = [{
        phoneNumber: "(111) 222-3344",
        businessNumber: "(112) 333-4567",
        name: "Name1"
      },
      {
        phoneNumber: "(111) 222-3344",
        businessNumber: "(112) 333-4567",
        name: "Name1"
      },
      {
        phoneNumber: "(111) 222-3344",
        businessNumber: "(112) 333-4567",
        name: "Name1"
      },
      {
        phoneNumber: "(111) 222-3344",
        businessNumber: "(112) 333-4567",
        name: "Name1"
      },
    ]
    
    let updatedArr = myArray.map((obj) => {
      obj.phoneNumber = obj.phoneNumber.split('-').pop()
      obj.businessNumber = obj.businessNumber.split('-').pop()
      return(obj);
    })
    console.log(updatedArr)

    0 讨论(0)
提交回复
热议问题