javascript 删除对象内的键值对
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 删除对象内的键值对相关的知识,希望对你有一定的参考价值。
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPassword = Object.keys(user)
.filter(key => key !== 'password')
.map(key => ({[key]: user[key]}))
.reduce((accumulator, current) =>
({...accumulator, ...current}),
{}
)
;
// userWithoutPassword becomes {name: 'Shivek Khurana', age: 23}
//cleaner way ?
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPassword = (({name, age}) => ({name, age}))(user);
//better way
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPassword = Object.keys(user)
.reduce((acc, key) => key === ‘password’ ?
acc : ({ …acc, [key]: user[key] }),
{}
);
//better way for collection of keys
const user = {name: 'Shivek Khurana', age: 23, password: 'SantaCl@use'};
const userWithoutPasswordAndAge = Object.keys(user)
.reduce((acc, key) => ['password', 'age'].indexOf(key) > -1 ?
acc : ({ …acc, [key]: user[key] }),
{}
);
以上是关于javascript 删除对象内的键值对的主要内容,如果未能解决你的问题,请参考以下文章