0

from my api I get the following array:

const apiResponse = [
        {
          "accountBillNumber": "123456789",
          "amountMoney": "0.00"
        },
        {
          "accountBillNumber": "987654321",
          "amountMoney": "0.01"
        },
    ];

I would like to write a very short code that will always change the value from accountBillNumber to value.replace(/(^\d{2}|\d{4})+?/g, '$1 '). Can it be done in a modern way with one line using es6 +?

3
  • Do you want to change the key accoutnBillNumber or its value? Commented May 26, 2020 at 8:46
  • 3
    "with one line using es6 +?" - Why always those "one line" requirements? Write maintainable code that you understand by just looking at it. If you need it to be as small as possible then use a minifier. Commented May 26, 2020 at 8:46
  • @Sid value. I would like change every accountBillNumber (1234567890, 9876543210) to (01 2345 6789, 98 7654 3210) using regex. Commented May 26, 2020 at 8:48

3 Answers 3

2

Use the .forEach() method.

const apiResponse = [
  {
    "accountBillNumber": "123456789",
    "amountMoney": "0.00"
  },
  {
    "accountBillNumber": "987654321",
    "amountMoney": "0.01"
  },
];

apiResponse.forEach(x => x.accountBillNumber = x.accountBillNumber.replace(/(^\d{2}|\d{4})+?/g, '$1 '));

console.log(apiResponse);

Or the .map() method to create a new array, as @Mamun's answer suggests.

const apiResponse = [
    {
      "accountBillNumber": "123456789",
      "amountMoney": "0.00"
    },
    {
      "accountBillNumber": "987654321",
      "amountMoney": "0.01"
    },
];

const result = apiResponse.map(x => {
  const { accountBillNumber, ...rest } = x;

  return {
    accountBillNumber: accountBillNumber.replace(/(^\d{2}|\d{4})+?/g, '$1 '),
    ...rest
  };
});

console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

You removed "amountMoney". I would like to save object, but only value in accountBillNumber I want to formatted using regex.
you could also destrucuture the object apiResponse.forEach(({ accountBillNumber: bill }) => bill = bill.replace(/(^\d{2}|\d{4})+?/g, '$1 '));
1

You can try the following way using Array.prototype.map():

const apiResponse = [
    {
      "accountBillNumber": "123456789",
      "amountMoney": "0.00"
    },
    {
      "accountBillNumber": "987654321",
      "amountMoney": "0.01"
    },
];
var res = apiResponse.map(v => { 
  return {
    amountMoney: v.amountMoney, 
    accountBillNumber: v.accountBillNumber.replace(/(^\d{2}|\d{4})+?/g, '$1 ')
  };
});
console.log(res)

1 Comment

I think it would be helpful to use rest parameters here. I included it in my answer. Because the APIs response could always change. And you wouldn't want to hard code all the objects properties.
0
apiResponse.map(x=>({...x,accountBillNumber:x.accountBillNumber.replace(/(^\d{2}|\d{4})+?/g, '$1')}))

Try this

Comments

Your Answer

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.