Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda