lodash set Object - Not able to create child object with an integer as key(Lotash Set Object-无法创建子对象,关键字为整数)
问题描述
我遇到使用Lodash设置对象的问题Lodash像这样设置
{
'288452': {
'57': 'value1',
'69': 'value2',
'01': 'value3'
}
}
下面是我尝试的代码
const _ = require from('lodash');
const obj = {};
_.set(obj, ['288452', '57'], 'value1');
// similarly for other values
但这会创建一个大小为57的数组作为"288452"的值。
我错过了什么吗?这是错误吗?
谢谢, Sudheesh CM
推荐答案
您应该在案例中使用_setWith
,因为您有数字键
const obj = {};
let a="288452",b="57";
_.setWith(obj, '['+a+']['+b+']', 'value1', Object);
console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
这篇关于Lotash Set Object-无法创建子对象,关键字为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!