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

140
Views
How can I concatenate this type of JSON in javascript?

How can I concatenate this json to obtain it:

complements = ["XYZ 3, CDE TR, AAA 5", "", "NDP 3, DDD FR"] ?

Each address can contain a set of complements which must be concatenated and separated by a comma.

P.s: I'm using javascript. P.s2: Complements can be null like in the second group in JSON.

[
    {
        "postalcode": "1234",
        "street": "ABC",
        "number": "1",
        "complement": [
            {
                "type": "B",
                "name": "XYZ",
                "description": "3"
            },
            {
                "type": "C",
                "name": "CDE",
                "description": "TR"
            },
            {
                "type": "D",
                "name": "AAA",
                "description": "5"
            }
        ]
    },
 {
        "postalcode": "444",
        "street": "No complements",
        "number": "5"
    },
    {
        "postalcode": "2222",
        "street": "BBB",
        "number": "2",
        "complement": [
            {
                "type": "E",
                "name": "NDP",
                "description": "3"
            },
            {
                "type": "F",
                "name": "DDD",
                "description": "FR"
            }
        ]
    }
];

My code I'm getting this.complementsList.forEach is not a function.

getComplement(addressesResponse){
    this.complementsList = JSON.parse(addressesResponse);

    this.complementsList.forEach((item) => {
    Object.defineProperty(item, 'complements', {
        get: function() { 
            return this.complement.map((c) => `${c.name} ${c.description}`).join(', '); }
    })
});

Source: https://salesforce.stackexchange.com/questions/367713/how-to-render-a-json-in-the-same-line-lwc

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

0

Having a javascript object, you can go through the keys of the object and combine some of them into strings

It will look something like this:

const jsonObject = [{...}, {...}, ...]
const complements = [];

jsonObject.forEach((item) => {
  let complement = item['complement'].reduce((result, currObj) 
     => result += (currObj.name+' '+currObj.description), "");
  complements.push(complement);
});

This is just an example. There are many ways to do it.

about 4 years ago · Juan Pablo Isaza Report

0

how i solved it :

arr.map((x)=>x.complement != null? (x.complement.map((y)=>y.name+' '+y.description)+"") :'');
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!