对象:
const item = {id:1}
功能:
// get is function, it can be _id or something else
const getId = (item) => item.id
const doSomthing = (obj, getId ) =>{
// here i wont to delete id from obj.
}
那么,有删除ID键的选项吗?
delete getId(item)
Here是我不会做的例子,所以有什么方法可以做到这一点?
答案 0 :(得分:0)
const item = {id:1, name: 'Joe', age: 23}
const item2 = {id:2}
const removeProp = (item, key) => {
key instanceof Array ?
key.forEach(k => delete item[k]) :
delete item[key]
return item;
}
const deleteIdES7 = ({ id, ...rest }) => rest;
const deleteId = (item) => removeProp(item, 'id')
console.log('id deleted', deleteId(item))
console.log('id deleted es7', deleteIdES7(item2))
console.log('strip the rest', removeProp(item, ['age', 'name']))