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

60
Views
Loop an array value inside json object and convert it to single string

so I have a JSON object here:

// TableData
{
    "0": {
        "damage_type": "Scratch",
        "regions": [
            "front side",
            "back side"
        ],
        "price": 100
    },
    "1": {
        "damage_type": "Scratch",
        "regions": [
            "front side",
            "back side,
            "right side"
        ],
        "price": 100
    }
}

And I want to convert it to something like this:

[
    {
        "damage_type": "Scratch",
        "regions": "front side;\nback side", // Notice the array become a single string and separated with ; and \n
        "price": 100
    },
    {
        "damage_type": "Scratch",
        "regions": "front side;\nback side;\nright side", 
        "price": 100
    }
]

Notice the array become a single string and separated with ; and \n

Does anybody have any idea how to reach that result?

My current (and unsuccessful) approach:

I am looping it like this:

  import data from '../data/table.json'


  const stringData = JSON.stringify(data)
  const tableData = JSON.parse(stringData)
  let processedData: any[] = []
  for(var i = 0; i <= Object.keys(data).length - 1 ; i++){
    processedData.push(tableData[i])
  }


console.log(processedData)

And the result is still like this:

[
    {
                "damage_type": "Scratch",
                "regions": ["Front Side", "Back side"],
                "price": 100
    },
    {
                "damage_type": "Scratch",
                "regions": ["Front Side", "Back side", "Right side"], // This part is still an array
                "price": 100
    }
]

To make everything easier please go https://onecompiler.com/javascript/3xb5y4qn8

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

0

Simple solution is using Object.values and Array.map

const TableData = {
    "0": {
        "damage_type": "Scratch",
        "regions": [
            "front side",
            "back side"
        ],
        "price": 100
    },
    "1": {
        "damage_type": "Scratch",
        "regions": [
            "front side",
            "back side",
            "right side"
        ],
        "price": 100
    }
}

const processedData = Object.values(TableData).map(row => ({
  ...row,
  regions: row.regions.join(';\n'),
}));

console.log(processedData)

about 4 years ago · Juan Pablo Isaza Report

0

Use following approach:

Object.values(TableData).map((item) => {
 return {...item, regions: item.regions.join(';\n')};
});
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!