javascript - lodash: Array of Objects -


i have node.js array of objects -

[     { sid: 1095, a: 484, b: 'someval1' },     { sid: 1096, a: 746, b: 'someval5' },     { sid: 1097, a: 658, b: 'someval7' },     { sid: 1098, a: 194, b: 'someval3' } ] 

a) possible update , delete members of array in-place based on sid , wouldn't slow things down performance-wise?

b) if needed chunk array , fire off async code against chunks ( insert db, 100 records @ time ) how async blend lodash's synchronous code?

pointers appreciated.

a) there 2 ways accomplish this. first, instantiate object using var newarr = _.keyby(arr, 'sid');. reference item want edit directly newarr[sid].a = 494. option use combination of _.indexof , _.find, var index = _.indexof(arr, _.find(arr, { sid: sid })); , edit using index arr[index].a = 494

b) lodash has method _.chunk.

var chunked = _.chunk(arr, 100); var promises = chunked.map(item => db.bulkinsert(item));  promise.all(promises)   .then(result => {     // whatever else want   }); 

i hope helpful. on b) little hard give specific advise without knowing how handling db operations (using orm, building queries hand, etc). basic idea (assuming using promises, should be) want generate array of promises representing each bulk insert. , once resolved.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -