1

I saw many answers, but I haven't been able to modify any to my need.

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);
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
onit
  • 2,275
  • 11
  • 25
  • There's no `id` property in `updatedComments`. – Barmar Oct 09 '22 at 22:31
  • In fact, it should update ```value``` if ```name``` is equal to ```Comments```, because there could be new custom fields, but this is the one to be updated. Thanks, @Barmar – onit Oct 09 '22 at 22:34

2 Answers2

1

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)
thebluetropics
  • 82
  • 2
  • 10
1

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612