最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论