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

232
Views
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 answers
Answer question

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 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!