Home > Net >  How can I update this nested object's value if one of its properties match in JS?
How can I update this nested object's value if one of its properties match in JS?

Time:10-10

I saw many answers, but I haven't been able to modify any to my need, so here I am, given my limited knowledge.

Object

{
  "id": "476ky1",
  "custom_id": null,
  "name": "Reunião com o novo Gerente de Vendas - Airton",
  "text_content": null,
  "description": null,
  "status": {
    "id": "p3203621_11svBhbO"
  },
  "archived": false,
  "creator": {
    "id": 3184709
  },
  "time_spent": 0,
  "custom_fields": [{
    "id": "36c0de9a-9243-4259-ba57-bd590ba07fe0",
    "name": "Comments",
    "value": "dsdsdsds"
  }],
  "attachments": []
}

Within custom_fields, if the property name's value is Comments, update the value property.

I've tried it like this, using this approach, for example, but it doesn't produce the expected result.

const updatedComment = [{ name: "Comments", value: "The comment is updated"}];
updateNestedObj(taskData, updatedComment)


function updateNestedObj(obj, updates) {
    const updateToApply = updates.find(upd => upd.id === obj.id);
    if (updateToApply) {
        obj.title = updateToApply.content;
        obj.note = updateToApply.note;
    }
    // Apply updates to any child objects
    for(let k in obj) {
        if (typeof(obj[k]) === 'object') {
            updateNestedObj(obj[k], updates);
        }
    }
}

Thanks in advance!

CodePudding user response:

You're using the wrong property names when you search updates for updateToApply, and then when assigning the value.

When you recurse on children, you need to distinguish between arrays and ordinary objects, so you can loop over the nested arrays. You also have to skip null properties, because typeof null == 'object'.

const updatedComment = [{
  name: "Comments",
  value: "The comment is updated"
}];


function updateNestedObj(obj, updates) {
  let updateToApply = updates.find(upd => upd.name == obj.name);
  if (updateToApply) {
    obj.value = updateToApply.value;
  }
  // Apply updates to any child objects
  Object.values(obj).forEach(child => {
    if (Array.isArray(child)) {
      child.forEach(el => updateNestedObj(el, updates));
    } else if (typeof(child) === 'object' && child != null) {
      updateNestedObj(child, updates);
    }
  });
}

const taskData = {
  "id": "476ky1",
  "custom_id": null,
  "name": "Reunião com o novo Gerente de Vendas - Airton",
  "text_content": null,
  "description": null,
  "status": {
    "id": "p3203621_11svBhbO"
  },
  "archived": false,
  "creator": {
    "id": 3184709
  },
  "time_spent": 0,
  "custom_fields": [{
    "id": "36c0de9a-9243-4259-ba57-bd590ba07fe0",
    "name": "Comments",
    "value": "dsdsdsds"
  }],
  "attachments": []
};

updateNestedObj(taskData, updatedComment)
console.log(taskData);

CodePudding user response:

Try this:

const updatedComment = [{ name: "Comments", value: "A new comment value" }]
// you can add as many updates as you want

function update(obj, updates) {
  for (const update in updates) {
    for (const field in obj.custom_fields) {
      if (obj.obj.custom_fields.name == update.name) {
        obj.obj.custom_fields.value = update.value
      }
    }
  }
}

update(obj, updatedComment)
  • Related