-1

i have an array of objects that gives out information in the below shown format.

personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
]

How can i Get the output to display the object as and so on. So basically only the phone number is edited in a sense to hide anything before the -

{
name:'Person1',
number:"2282",
membership:"standard"
}
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
TreeHuggerRick
  • 402
  • 2
  • 5
  • 18
  • Possible duplicate of https://stackoverflow.com/questions/573145/get-everything-after-the-dash-in-a-string-in-javascript – Calvin Nunes Oct 16 '19 at 18:41
  • @CalvinNunes i tried the first one but that only seems to return the number i guess but i still want to return the whole array back with the modified number. – TreeHuggerRick Oct 16 '19 at 18:43
  • i came to give you a upvote inspite of the downvoters. Il do it for the duplicate markers too. – Roboman Robo Aug 09 '20 at 17:19

4 Answers4

2

All you need to do is to loop the array and do your manipulation

const edited = personArray.map(e => {
  e.number = e.number.substring(e.number.indexOf('-') + 1);
  return e;
});

console.log(edited);
<script>
const personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
]
</script>

Note that you could also use forEach and manipulate personArray directly without mapping it to a new variable.

baao
  • 71,625
  • 17
  • 143
  • 203
1

You can manipulate the phone number as you wish using a simple .replace call with regex.

let phone = "(770) 556-2282";
let phone2 = phone.replace(/.*-/, '');
console.log(phone2);

To apply this to all the objects in the array, simply iterate them with .forEach (or .map if you don't wish to modify the original objects and array).

let personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
];

personArray.forEach(p => p.number = p.number.replace(/.*-/, ''));

console.log(personArray);
junvar
  • 11,151
  • 2
  • 30
  • 46
1

You can do that by using Array.prototype.map.

const personArray = [{
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
  {
    name: 'Person1',
    number: "(770) 556-2282",
    membership: 'standard'
  },
];

const updatedArray = personArray.map(user => {
  const indexToRemove = user.number.indexOf('-');
  const number = user.number.slice(indexToRemove + 1);
  return {...user, number};
});

console.log(updatedArray);
Diamond
  • 3,470
  • 2
  • 19
  • 39
1

You have to do like this:

for(let i = 0; i < personArray.length; i++){
    let indexChar = personArray[i].number.indexOf('-');
    personArray[i].number = personArray[i].number.substring(indexChar + 1);
}

Search where is the '-' char and than take the right side of the number.

Simone Boccato
  • 199
  • 1
  • 14