Skip to content

Instantly share code, notes, and snippets.

@joepie91
Created February 10, 2020 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joepie91/67acc5ab3af5f86697819d4db2627b7e to your computer and use it in GitHub Desktop.
Save joepie91/67acc5ab3af5f86697819d4db2627b7e to your computer and use it in GitHub Desktop.
const immutableCollection = require("./");
let items = [{
id: 1,
color: "blue"
}, {
id: 2,
color: "red"
}, {
id: 3,
color: "red"
}, {
id: 4,
color: "red"
}, {
id: 5,
color: "blue"
}, {
id: 6,
color: "blue"
}, {
id: 7,
color: "red"
}];
let collection = immutableCollection(items);
let modified = collection.modify((collection) => {
return collection
.subset((blueItems) => {
return blueItems
.select((item) => item.color === "blue")
.remove();
})
.subset((oddItems) => {
return oddItems
.select((item) => item.id % 2 === 1)
.map((item) => ({ ... item, color: "green" }));
})
.map((item) => ({ ... item, id: item.id * 2 }));
});
console.log(modified);
[ { id: 4, color: 'red' },
{ id: 6, color: 'green' },
{ id: 8, color: 'red' },
{ id: 14, color: 'green' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment