0

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 "-" to only show the last 4 digits?

I was able to use the map to extract the businessNumber but how can i split an return them both in the desired format?

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 arr1 = myArray.map(function(obj) {
 return obj.businessNumber.split('-').pop()
})
console.log(arr1)

Thank you in advance.

asked Oct 16, 2019 at 16:16
4
  • 1
    Possible duplicate of Get everything after the dash in a string in javascript Commented Oct 16, 2019 at 16:17
  • would you be able to provide an example of the output you want? It's not clear from the question. Thanks Commented Oct 16, 2019 at 16:22
  • @Finnnn please check the updated code, i made changes to the return line. but what would be the cleanest way of putting this on business and phone number together at the same time? Commented Oct 16, 2019 at 16:25
  • @CalvinNunes Thank you that helped but what would be the cleanest way of putting this on business and phone number together at the same time? Commented Oct 16, 2019 at 16:25

2 Answers 2

2

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() 
 }
})
answered Oct 16, 2019 at 16:28

Comments

1

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)

answered Oct 16, 2019 at 16:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.