【发布时间】:2019-12-27 05:56:28
【问题描述】:
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 is already declared
【问题讨论】:
-
您的代码运行良好。它记录
undefined,因为您正在记录removeItem的结果(它不返回任何内容),但是如果您在removeItem(comments)之后记录comments,您会看到第一项不再存在。您所描述的 "commentsis already declared" 错误在您共享的代码中不存在。 -
Console.log 在您调用 removeItem 后退出 cmets 数组,它会显示您想要的样子
标签: javascript object ecmascript-6