javascript - lodash set Object - Not able to create child object with an integer as key - Stack Overflow

I'm facing issues setting an object using Lodash set like this,{'288452':{ '57�

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 badges
Add a ment  | 

2 Answers 2

Reset to default 9

You 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');.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745199134a4616239.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信