Replace in array using lodash

lodash replace object in array
import lodash
how to use lodash
lodash chain
lodash online
lodash filter array of objects
lodash compare objects

Is there an easy way to replace all appearances of an primitive in an array with another one. So that ['a', 'b', 'a', 'c'] would become ['x', 'b', 'x', 'c'] when replacing a with x. I'm aware that this can be done with a map function, but I wonder if have overlooked a simpler way.

In the specific case of strings your example has, you can do it natively with:


Where "," is some string that doesn't appear in the array.

That said, I don't see the issue with a - it sounds like the better approach since this is in fact what you're doing. You're mapping the array to itself with the value replaced.,function(el){
     return (el==='a') ? 'x' : el;

template function, Creates an array of array values not included in the other given arrays using SameValueZero for Its creation may be customized by replacing the _.​memoize. In my project I had to find a way to replace the contents of an array without creating a new array, so I thought of setting the array length to 0 then appending the elements one by one. I wondered if it was possible to add an utility to lodash, say, _.fill(array, newArray)?

I don't know about "simpler", but you can make it reusable

function swap(ref, replacement, input) {
    return (ref === input) ? replacement : input;

var a = ['a', 'b', 'a', 'c'];, _.partial(swap, 'a', 'x'));

_.replace(collection, match, new) · Issue #1022 · lodash , A value with which matched items will be replaced; A function which takes in the old value and returns the new value. _.replace(collection, match,  Deep replace a value within an object or array (using lodash or underscore) - replacePropertyValue.js

If the array contains mutable objects, Its a straight forward with lodash find function.

var arr = [{'a':'a'}, {'b':'b'},{'a':'a'},{'c':'c'}];    

while(_.find(arr, {'a':'a'})){
  (_.find(arr, {'a':'a'})).a = 'x';

console.log(arr); // [{'a':'x'}, {'b':'b'},{'a':'x'},{'c':'c'}]

Update array element at index · Issue #2053 · lodash/lodash · GitHub, I'm using lodash and ES6 and most of the time these 2 combined solves most of my array.splice(index, 1, replacement) mutates the array. Right now, Lodash is the most depended-on npm package, but if you’re using ES6, you might not actually need it. In this article, we’re going to look at using native collection methods with

Another simple solution. Works well with arrays of strings, replaces all the occurrences, reads well.

var arr1 = ['a', 'b', 'a', 'c'];
var arr2 =, _.partial(_.replace, _, 'a', 'd'));
console.log(arr2); // ["d", "b", "d", "c"]

10 Lodash Features You Can Replace with ES6, 10 Lodash Features You Can Replace with ES6 structure, then you can use the spread operator to clone the array before calling reverse: Creates an array of unique values that are included in all given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array. Since. 0.1.0 Arguments [arrays] (Array): The arrays to inspect. Returns (Array): Returns the new array of intersecting values. Example

lodash replace, If I want to replace more than one instance with the lodash _.replace will work well on most objects in general while the method is  Install n_ for Lodash use in the Node.js < 6 REPL. Why Lodash? Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc. Lodash’s modular methods are great for: Iterating arrays, objects, & strings; Manipulating & testing values; Creating composite functions. Module Formats

10 Lodash functions everyone should know, In short, Lodash is a JS helper library for arrays, strings and objects. iterating through an array with a loop to find a specific object, we can simply use _.find. If the key 'name' appears in array a, I do not want it to appear in b. Remove key from object/value using Lodash. Ask Question Browse other questions tagged

Replace an item in an array, by number, without mutation in , There's a much faster way and that's to use slice and it actually looks nicer too: function replaceAt(array, index, value) { const ret = array.slice(0);  If using a complex object (or even a simple one) and you can use es6, Array.prototype.findIndex is a good one. For the OP's array, they could do, const index = items.findIndex(x => x === 3452) items[index] = 1010 For more complex objects, this really shines. For example,

  • I think the purpose of a library like Lo-Dash is not to enable you to write the most code-golfy solution you can possibly write, but to write the most natural and idiomatic solution. And what could be more natural and idiomatic than map() in this case? It's the perfect tool for the job.
  • @Tomalak _(array).filter('someKey').replace('a', 'x') looks very natural to me
  • But that would not be logical - I would expect an operation named "filter" to reduce number of elements in the input. Also: It's not about the looks. It's about the operation you want to perform on the data, and to me that is "mapping a list of N values a list of N values (some of which by accident happen to be the same as the input)".
  • Use findIndex from Lodash then replace the element at the found index
  • The question was about primitives not just strings. Also I want to use lodash, as this will be one step in a longer chain, where the array data is manipulated in.
  • @AndreasKöberle First of all using a particular library is a mean and not a goal. Second of all - in that case is what you're looking for, underscore/lodash has no specific replace/swap functionality on collections, you can add it to the lodash prototype yourself if you'd like.
  • is really fit to this use case, there's no need to look for anything else.