TehShrike / deepmerge

A library for deep (recursive) merging of Javascript objects
MIT License
2.75k stars 216 forks source link

How to delete an element from an array with deepmerge. #243

Open ghost opened 2 years ago

ghost commented 2 years ago

Hi, I would like to be able to remove a value from my array but with deepmerge the array values only add up. Does anyone have an idea how to solve this problem?

const dp = require('deepmerge');
const a = ['test0', 'test1', 'test2'];
const b = ['test1'];
dp(a, b); // outpout => ['test0', 'test1', 'test2', 'test1'] or I want ['test0', 'test2']
RebeccaStevens commented 2 years ago
const options = {
  arrayMerge: (x, y) => {
    const result = x.slice();
    for (const element of y) {
      const index = x.indexOf(element);
      if (index >= 0) {
        result.splice(index, 1);
      }
    }
    return result;
  }
};

const result = deepmerge(a, b, options);