[英]Lodash collection bulk update
我想要這樣的代碼:
_.bulkUpdate(myCollection,'meta.type','new');
換句話說,我想通過字符串或函數更新集合中的字段,我知道此函數存在,但不知道其名稱
您可以使用_.forEach()
迭代數組,然后使用_.set()
值或_.update()
進行批量更新,使用以下函數:
var arr = [{ id: 1 }, { id: 2 }, { id: 3 }]; _.forEach(arr, function(item, index) { _.set(item, 'meta.type', 'new'); // to set a value _.update(item, 'meta.value', function() { return index; }); // to set with a function }); console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.3/lodash.min.js"></script>
嘗試使用_.update
或_.updateWith
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.