admin管理员组文章数量:1415111
I'm facing issues setting an object using Lodash set like this,
{
'288452': {
'57': 'value1',
'69': 'value2',
'01': 'value3'
}
}
Below is the code I tried,
const _ = require from('lodash');
const obj = {};
_.set(obj, ['288452', '57'], 'value1');
// similarly for other values
But this creates an array of size 57 as the value for '288452'.
Am I missing anything? Is this a bug?
Thanks, Sudheesh CM
I'm facing issues setting an object using Lodash set like this,
{
'288452': {
'57': 'value1',
'69': 'value2',
'01': 'value3'
}
}
Below is the code I tried,
const _ = require from('lodash');
const obj = {};
_.set(obj, ['288452', '57'], 'value1');
// similarly for other values
But this creates an array of size 57 as the value for '288452'.
Am I missing anything? Is this a bug?
Thanks, Sudheesh CM
Share Improve this question asked Mar 27, 2017 at 11:01 sudheeshcmsudheeshcm 3,4384 gold badges16 silver badges22 bronze badges2 Answers
Reset to default 9You should use _setWith
in your case because you have numeric keys
const obj = {};
let a="288452",b="57";
_.setWith(obj, '['+a+']['+b+']', 'value1', Object);
console.log(obj);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Try with this syntax _.set(x, '288452.57', 'foo');
.
本文标签: javascriptlodash set ObjectNot able to create child object with an integer as keyStack Overflow
版权声明:本文标题:javascript - lodash set Object - Not able to create child object with an integer as key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745199134a2647286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论