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

本文标签: javascriptlodash set ObjectNot able to create child object with an integer as keyStack Overflow