gpt4 book ai didi

javascript - 如何从 ids 对象中删除项目

转载 作者:行者123 更新时间:2023-11-28 03:37:10 25 4
gpt4 key购买 nike

const comments = {
'1': { id: '1', text: 'please add code examples' },
'2': { id: '2', text: 'examples would be great for this article' },
'3': { id: '3', text: 'hi there' },
};

const removeItem = items => {
delete items[1];
};

console.log(removeItem(comments));

我需要根据项目的唯一标识符删除项目。这显示 comments 已声明

最佳答案

如果你想删除一个特定的id,并且我希望你的对象键与它所拥有的id相同,你可以更改删除项以接受id参数并稍微修改一下函数,这样它就可以了会根据key属性删除对象。

var comments = {
1: { id: '1', text: 'please add code examples' },
2: { id: '2', text: 'examples would be great for this article' },
3: { id: '3', text: 'hi there' },
};

var removeItem = (items, id) => {
delete items[id];
};

removeItem(comments, 1);

console.log(comments);

关于javascript - 如何从 ids 对象中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com