Skip to content

Instantly share code, notes, and snippets.

@joepie91
Created February 4, 2020 11:19
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/107b793d46e4ac32584cc026d1e15e91 to your computer and use it in GitHub Desktop.
Save joepie91/107b793d46e4ac32584cc026d1e15e91 to your computer and use it in GitHub Desktop.
"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);
[ { 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