score:3

Accepted answer
// Define this somewhere
const INITIAL_STATE = {
  "some_id1": [
    {
      "id": 93979
    },
    {
      "id": 93978
    }
  ],
  "some_id2": [
    {
      "id": 93961
    },
    {
      "id": 93960
    }
  ]
};

// State initialization
const [items, setItems] = React.useState(INITIAL_STATE);

// Handler to remove the nested object with matching `id`
const removeByNestedId = (id, items) => {
  const keys = Object.keys(items);
  const updatedItems = keys.reduce((result, key) => {
    const values = items[key];
    // Since, we want to remove the object with matching id, we filter out the ones for which the id did not match. This way, the new values will not include the object with id as `id` argument passed.
    result[key] = values.filter(nestedItem => nestedItem.id !== id)
    return result;
  }, {});
  setItems(updatedItems);
}

// Usage
removeByNestedId(93961, items);

score:0

const items = {
  "some_id1": [
    {
      "id": 93979,   
    },
    {
      "id": 93978,
    }
  ],
  "some_id2": [
    {
      "id": 93961,
    },
    {
      "id": 93960,
    }
  ]
}

const singleItemId = 93979;
for (const k in items) items[k] = items[k].filter(x => x.id !== singleItemId);

console.log(items);
//setItems(items)

score:0

setData(d=>d.filter(item=>item.id !== id));

score:1

const data = {"some_id1": [{"id": 93979},{"id": 93978}],"some_id2": [{"id": 93961},{"id": 93960}]}
const remove = ({id, data}) => {
  return Object.entries(data).reduce((prev, [nextKey, nextValue]) => {
    return {...prev, [nextKey]: nextValue.filter(({id: itemId}) => id !==  itemId)}
  
  }, {})

}

console.log(remove({id: 93961, data}))

Related Query

More Query from same tag