Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue Jan 30, 2022 · 1 comment
Open

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

ghost opened this issue Jan 30, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Jan 30, 2022

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']
@ghost ghost changed the title How to delete an element from an array. How to delete an element from an array with deepmerge. Jan 30, 2022
@RebeccaStevens
Copy link

RebeccaStevens commented Jan 30, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant