-
-
Save joepie91/107b793d46e4ac32584cc026d1e15e91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
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 | |
.select((item) => item.color === "blue") | |
.modify((selection) => { | |
return selection | |
.filter((item) => item.id % 2 === 0) | |
.map((item) => ({ ... item, color: "green" })) | |
.addAfter({ id: 13, color: "yellow" }) | |
.addAfter({ id: 14, color: "purple" }) | |
.addBefore({ id: 0, color: "orange" }); | |
}); | |
console.log(modified); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ { id: 2, color: 'red' }, | |
{ id: 3, color: 'red' }, | |
{ id: 4, color: 'red' }, | |
{ id: 0, color: 'orange' }, | |
{ id: 6, color: 'green' }, | |
{ id: 13, color: 'yellow' }, | |
{ id: 14, color: 'purple' }, | |
{ id: 7, color: 'red' } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment