2017-10-10 80 views
0

這是我的目標:如何使用lodash從對象中刪除屬性?

var obj = { 
    "flag": false, 
    "text": "good text", 
    "someArray": [ 
    { 
     "questionId": 11, 
     "text": "qwe", 
     "TypeId": "B", 
     "source": "oooooo" 
    }, 
    { 
     "questionId": 12, 
     "text": "zxc", 
     "TypeId": "A", 
     "source": "pppppp" 
    }, 
    { 
     "questionId": 13, 
     "text": "asd", 
     "TypeId": "D", 
     "source": "cccccc" 
    } 
    ] 
} 

我試圖做的是從對象someArray性能TypeIdsource刪除。我如何用lodash做到這一點?我正在嘗試使用_.pick,但我不知道如何在someArray中使用這個。

+1

映射他們沒有返回給定性質的對象,爲什麼不與本機JS做呢? – RomanPerekhrest

+1

它與lodash一樣簡單而優雅嗎? – DiPix

+0

只需用'_.pick'函數將'.map'放在數組上 – Bergi

回答

2

你想用的_.pick相反,這是_.omit通過與_.map

var obj = { 
 
    "flag": false, 
 
    "text": "good text", 
 
    "someArray": [{ 
 
    "questionId": 11, 
 
    "text": "qwe", 
 
    "TypeId": "B", 
 
    "source": "oooooo" 
 
    }, { 
 
    "questionId": 12, 
 
    "text": "zxc", 
 
    "TypeId": "A", 
 
    "source": "pppppp" 
 
    }, { 
 
    "questionId": 13, 
 
    "text": "asd", 
 
    "TypeId": "D", 
 
    "source": "cccccc" 
 
    }] 
 
} 
 

 
obj.someArray = _.map(obj.someArray, o => _.omit(o, ['TypeId', 'source'])); 
 

 
console.log(obj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

相關問題