码迷,mamicode.com
首页 > 其他好文 > 详细

对象属性剔除

时间:2020-07-21 14:03:15      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:for   null   lse   cti   include   reac   power   string   pre   

应用场景很简单,当你需要使用一个对象,但想移除部分属性时,可以使用该方法。同样的,你可以实现一个对象属性选取方法。

/**

* @param {object} object

* @param {string[]} props

* @return {object}

*/

function omit(object, props=[])

{

let res = {}

Object.keys(object).forEach(key=>{

if(props.includes(key) === false){

res[key] = typeof object[key] === ‘object‘ && object[key] !== null ? jsON.parse(jsON.stringify(object[key])): object[key]

}

})

return res

}

使用

let data = {  id: 1,  title: ‘xxx‘,  comment: []}

omit(data, [‘id‘]) // {title: ‘xxx‘, comment: []}

 

对象属性剔除

标签:for   null   lse   cti   include   reac   power   string   pre   

原文地址:https://www.cnblogs.com/blhgys/p/13354337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!