Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Convert the string to an object

I want to convert a string to an object.

My string is something like 7541550448872:1:1:monthly,42397357179112:1.

I want the result to be something like this:

'items': [{
    'id': 7541550448872,
    'quantity': 1,
    "properties": {
      "shipping_interval_unit_type": "monthly",
      "shipping_interval_frequency": "1"
    },
  },
  {
    'id': 42397357179112,
    'quantity': 1,
  }
]

I was told to use functions like split() and Regex but I could not get it to work. Any idea how to do this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For me, probably the most preferred method is using Array.split(). You split the array by :, and then construct the JSON by indexing the array. See the code snippet below to see this in action. Also, I'm not sure if you wanted a , between the month and the id or if was just a typo. My code below uses the ,.

const str = '7541550448872:1:1:monthly,42397357179112:1';
const arr = str.split(':');
const json = {
  'items': [{
      'id': Number(arr[0]),
      'quantity': Number(arr[1]),
      "properties": {
        "shipping_interval_unit_type": arr[3].split(',')[0],
        "shipping_interval_frequency": arr[2]
      },
    },
    {
      'id': Number(arr[3].split(',')[1]),
      'quantity': Number(arr[4]),
    }
  ]
};

console.log(json);

Hoped this helped!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!