- let objData = {
- name: '小草莓',
- age: 18,
- sex: '女',
- hobby: ['学习']
- }
- let objDefault = {
- name: '小草莓',
- age: 18
- }
- /**
- * 从对象中排除指定的属性并返回新对象
- * @param {Object} originObj 源对象
- * @param {Array} arr 指定排除的属性名数组,如:['name', 'text']
- * @returns {Object}
- */
- function omit(originObj, arr) {
- if (!originObj) {
- return {}
- }
- if (!arr || !Array.isArray(arr) || !arr.length) {
- return originObj
- }
-
- const res = {}
- Object.keys(originObj).forEach(k => {
- !arr.includes(k) && (res[k] = originObj[k])
- })
- return res
- }
-
- let keys = Object.keys(objDefault)
- console.log('omit最后结果', omit(objData, keys))
- // omit最后结果 { sex: '女', hobby: ['学习'] }