假设我有一个对象
var users = [
{ Mike: 'true' },
{ Tony: 'True' },
{ Ismael: 'RU' }
];
我有这个问题,我想规范化我的对象,基本上用 bool 值 true
替换“true”或“True”,其他任何东西都应该是 false
。
顺便说一句,我可能对 users
的语法有误,chrome 告诉我 users.constructor == Object
而不是 Array
。
如何使用 lodash 实现此目的?
最佳答案
在 Lodash 中,您可以使用 _.mapValues
:
const users = [
{ Mike: 'true' },
{ Tony: 'True' },
{ Ismael: 'RU' },
];
const normalisedUsers = users.map(user =>
_.mapValues(user, val => val.toLowerCase() === 'true')
);
console.log(normalisedUsers);
<script src="https://cdn.jsdelivr.net/lodash/4.16.3/lodash.min.js"></script>
关于javascript - 替换对象值 lodash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40393880/