rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.37k stars 195 forks source link

Make `without` support removing item from an array #239

Open Aaronius opened 6 years ago

Aaronius commented 6 years ago

The without method allows a property to be a removed from an object. I propose that it should also allow an item to be removed from an array:

const fruits = Immutable(['apple', 'banana', 'cherry']);
fruits.without(1); // ['apple', 'cherry']
markhicken commented 6 years ago

I totally agree! Do you think it would also make sense to allow removing by value?

const fruits = Immutable(['apple', 'banana', 'cherry']);
fruits.without('banana'); // ['apple', 'cherry']
Aaronius commented 6 years ago

No, because it leads to ambiguity:

const fruits = Immutable([3, 2, 1]);
fruits.without(1); // ?
markhicken commented 6 years ago

Ok then, maybe it's a rabbit hole but it seems like most of the time, you would know the value but not the index. How about something like this?

const fruits = Immutable(['apple', 'banana', 'cherry']);
fruits.without('banana', { byValue: true }); // ['apple', 'cherry']
SunXinFei commented 6 years ago

@Aaronius @markhicken but like this data: const fruits = Immutable([{a:['aaa','bbb','ccc']}, 'banana', 'cherry']); how can i remove 'bbb'. question 2: the without fn will not change the index and the array will lost the array fn , so after call without fn ,the array can not call .map ,.flatMap, _.forEach..... ,only can call for(let i in array), just like this : image