Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda