I already see the lodash documentation but I don't know what function do I need to use to solve my problem. I have array
const arr = [
{name: 'john'},
{name: 'jane'},
{name: 'saske'},
{name: 'jake'},
{name: 'baki'}
]
I want to add {name: 'ace'}
before saske. I know about splice in javascript, but I want to know if this is possible in lodash.
Currently lodash not have that. You can use this alternate
arr.splice(index, 0, item);
const arr = [
{name: 'john'},
{name: 'jane'},
{name: 'saske'},
{name: 'jake'},
{name: 'baki'}
]
arr.splice(2, 0, {name: 'ace'})
console.log(arr)
There is no support of such functionality in lodash : see issue.
If you still want it to look like done with lodash, then you can do it like this way.
fields = [{name: 'john'},
{name: 'jane'},
{name: 'saske'},
{name: 'jake'},
{name: 'baki'}
];
_.insert = function (arr, index, item) {
arr.splice(index, 0, item);
};
_.insert(fields,2,{'name':'ace'});
console.log(fields);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With