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

160
Vistas
How to merge a gejoson properties with an array based on the same key

I have two objects with same length and a common property edge_id on each Id. I would like to add to the second object to the properties of the first object.

I have a MapLibre map that I would like to add some properties coming from an API (the second object).

First object

 [
    {
        "type": "Feature",
        "properties": {
            "edge_id": 67135,
            "color": "#f7fabf"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [ ]
        
        }
    },
    {
        "type": "Feature",
        "properties": {
            "edge_id": 15984,
            "color": "#fcd6a4"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [ ]
        }
    },
]

Second object

[
    {
        "edge_id": 67135,
        "name": "R12",
        "length": 0.14907826598895346,
        "speed": null,
        "lanes": null
    },
    {
        "edge_id": 15984,
        "name": "Pont de Sully",
        "length": 0.01577450403315043,
        "speed": 30,
        "lanes": 2
    },
]

I want the below output for edge_id : 67135:

{
    "type": "Feature",
    "properties": {
        "edge_id": 67135,
        "color": "#f7fabf",
        "name": "R12",
        "length": 0.14907826598895346,
        "speed": null,
        "lanes": null
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [ ]
    }
},
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would first collect the second object's items by edge_id so that we can look them up in constant time later on:

const secondObjectByEdgeId = secondObject.reduce(
  (o, x) => ({ ...o, [x.edge_id]: x }),
  {}
);

And then iterate over the first object's items, enhancing their properties field with what we just collected above:

const merged = firstObject.map((x) => ({
  ...x,
  properties: {
    ...x.properties,
    ...secondObjectByEdgeId[x.properties.edge_id]
  }
}));

Note that the above approach will simply ignore any item in the second object whose edge_id is not present in the first object.


Same algorithm relying on side-effects:

const secondObjectByEdgeId = secondObject.reduce(
  (o, x) => {
    o[x.edge_id] = x;
    return o;
  },
  {}
);

firstObject.forEach((x) => {
  Object.assign(x.properties, secondObjectByEdgeId[x.properties.edge_id]);
});

firstObject has now been updated directly.

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