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

225
Vistas
JS reduceRight is causing arrays to become objects, how can I fix the logic?

I have an json object:

json = {
    "array": [80, 50]
}

A third party jsoneditor, returns a path variable as a list of strings or index to the value. I use event listeners to detect changes to the value and I store those changes in a separate object to later submit to a form.

For example, if I change 50 to 60, this is what is returned:

node.path = ["array", 1]
node.value = 60

I am using reduceRight() to normally map the changes to the object to the changed object, however when the value is an Array it is converting the array to an object.

let delta = node.path.reduceRight((obj, elem) => ({[elem]: obj}), node.value)

//returns
delta = {array: {1: 60}}

//instead of 
delta = {array: [80, 60]}

How can I check the original json and if the field is an Array object, don't reduce this into an object but keep the original Array and update the Array with the changed value within the Array? I need to submit the entire array into the form.

EDIT: I know that I can do some custom logic within the reduce to check the original json and get the unchanged part of the array. I just dont know how to achieve that

let delta = node.path.reduceRight( function (obj, elem) { 
    //some logic here to account for arrays and get original array list and change only slected index
    else { 
        return {[elem]: obj}), node.value 
    } 
});

EDIT: Alternatively, how can can I get the nested keys from the node.path and find the original array and then just update the array index? The path can sometimes be nested sothe path is always going to be the Array-like structure.

//this does not work
let orig_key_val = json[node.path]

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This seems to work, finally... I used a regular reduce() and check if the key is an Array if it is store my original Array into a tmp object and save that key. The next time the reduce() comes around, if the index is on the last path element then set the tmp[tmp_key][key] to my target value and return the adjusted for Array object instead.

I can see how this will not work for nested json objects but for now I dont have any... Unless someone can show me how to figure that one out this is my implementation for now.

let tmp = {};
let tmp_key;
let delta = node.path.reduce((val, key, index, array) => {
    if (Array.isArray(json[key])) {
        tmp[key] = json[key]
        tmp_key = key;
    } else if (Object.keys(tmp).length !== 0 && index === node.path.length-1) {
        tmp[tmp_key][key] = node.value;
        return tmp
    } else
        return {[key]: val}

}, node.value);
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