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