Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

updateWhere to update a sublist #165

Open
Methuselah96 opened this issue Oct 17, 2020 · 0 comments
Open

updateWhere to update a sublist #165

Methuselah96 opened this issue Oct 17, 2020 · 0 comments

Comments

@Methuselah96
Copy link

From @lpeliberty on Tue, 26 Nov 2019 08:20:07 GMT

What happened

Hello, I was trying to update a sublist of a List, but I've not found a proper way to do this (using only one method).
Example : I have an Immutable cart with products and each products have a category and a price :

{
  cart: {
    products: [
      {
        category: 'category1',
        price: '10',
      },
      {
        category: 'category2',
        price: '20',
      },
    ]
  }
}

If I want to return the whole cart, and update only product where category is category1, I can do a map + update, a filter + merge like :

return cart.get('products').map(product => {
  if (product.category === 'category1') return product.set('price', 0);
  return product;
})

But with that, I only return products and not cart, so I have to merge... And sometimes I'm returning product without changes because it does not match my condition, it's not bad but not perfect right ?

return cart.updateWhere(
  'products',
  product => product.category === 'category1',
  product => product.set('price', 0),
})

Or with a updateInWhere for more complexe objects, It would be awesome for applications like react-redux (my use case).

Is it a bad idea ? Did I missed something in immutableJS docs that can do it ?

Thx to read.

Copied from original issue: immutable-js#1752

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

No branches or pull requests

1 participant