0

I have to update the object based on value change from dropdown and input for each index .

Below is the sample

datas[5] = { "qty_sized": "1", "resolution": "5", "status": "", "order": 1342 };

where [5] is dynamic from response.

I have and object mydata and inside that I have a object items.

I push array to object items, with assign

Object.assign(mydatadata.items, datas);

Now mydata.items has an array set,

items {
    1 { qty_auth: "", resolution: "5", status: "", order: "1495"},
    5 { qty_sized: "1", resolution: "4", status: "", order: "1485"}
}

the resolution default value is initailly 5 and status is empty based on API.

I have one input filed where qty_ value is set. 2 dropdown for each index, one which changes resolution which has values 3,4,5, other which changes status "allow","Yes","No".

When i chnage one dropdown i want to update resolution to 3 and when i update second dropdown i want to update status to "allow".

But when i change dropdown for resolution value, status becomes "", and when i change status dropdown the resolution values goes the default 5.

I want to update and keep the value on change like this :

const mydatadata = {
      items: {
        1: {qty_auth: "", resolution: "3", status: "allow", order: "1495"},
        5: {qty_sized: "1", resolution: "5", status: "", order: "1485"}
      }
    };

The below code is what i tried

var datas = [];
var mydatadata = {};
mydatadata.items = {};
var arr3;
let dataV = [];
var arr4;
let dataVs = [];
let rdata = {};
rdata.items = {};

      overrideValue = (index, rId, itemid, override, resfield, statusValue) => {
        if(override != "")
        console.log(statusValue);
        let stat = statusValue;
        console.log(this.state.job.resolutions);
        let res = this.state.job.resolutions.find(x => x.option_label === resfield.toLowerCase()).option_id;
        for (var i = 0; i < datas.length; i++) {
            console.log(datas.length);
            if (datas[i] == 1) datas.push(5);
        }
        datas[rId] = {
            [stat]: override,
            "resolution": res.toString(),
            "status": "",
            "order": itemid
        };

        arr3 = datas.map((item, i) => Object.assign({}, item, dataV[i]));
        console.log(arr3);
        console.log(datas);
        Object.assign(rmadata.items, arr3);
        mydatadata.items = Object.fromEntries(
            Object.entries(mydatadata.items)
                .filter(([_, item]) => {
                    const qtyKey = Object.keys(item).find(key => key.startsWith('qty_'));
                    if (qtyKey && !item[qtyKey]) return false;
                    return true;
                })
        );
    }

   onChangeRes = (stringValue) => (event) => {
        console.log(event.target.value);
        console.log(stringValue);
  
        dataVs[stringValue] = {
            "resolution": event.target.value,
        };
        Object.assign(rdata.items, dataVs) 
        arr4 = datas.map((item, i) => Object.assign({}, item, dataVs[i]));
        Object.assign(mydatadata.items, arr4);
        console.log(mydatadata);
    };

    onChangeStatus = (stringValue) => (event) => {
        console.log(event.target.value);
        console.log(stringValue);
        dataV[stringValue] = {
            "status": event.target.value,
        };
        Object.assign(rdata.items, dataV)
        console.log(dataV)
        console.log(datas);
        // Array.prototype.push.apply(datas, data); 
        arr3 = datas.map((item, i) => Object.assign({}, item, dataV[i]));
        console.log(arr3);
        console.log(datas);

        Object.assign(mydatadata.items, arr3);
        console.log(rmadata);
    };
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [Update the object value based on array index](https://stackoverflow.com/questions/69814946/update-the-object-value-based-on-array-index) – Nacho Justicia Ramos Nov 02 '21 at 23:10
  • HI, i only have created that question since i was not getting any response for the above @NachoJusticiaRamos – Shantal Kaula Nov 03 '21 at 10:18

0 Answers0