Skip to content

Instantly share code, notes, and snippets.

@fmarcia
Last active February 15, 2018 18:59
Show Gist options
  • Save fmarcia/97e6c61291032eaeb9116c28f36b14d6 to your computer and use it in GitHub Desktop.
Save fmarcia/97e6c61291032eaeb9116c28f36b14d6 to your computer and use it in GitHub Desktop.
Remove items
var limit = 4;
arr = arr.filter(e => {
if (e.a < limit) {
delete obj[e.n];
} else {
return true;
}
});
// initialization
var arr = [
{ n: "a", a: 1, b: 1 },
{ n: "b", a: 2, b: 2 },
{ n: "c", a: 3, b: 3 },
{ n: "d", a: 4, b: 4 },
{ n: "e", a: 5, b: 5 }
];
var obj = {};
arr.forEach(e => {
obj[e.n] = e;
});
// remove 1st items
var limit = 3;
var count = 0;
var start = -1;
arr.forEach((e, i) => {
if (e.a < limit) {
if (start === -1) {
start = i;
}
count += 1;
delete obj[e.n];
}
});
arr.splice(start, count);
> arr
[ { n: 'c', a: 3, b: 3 },
{ n: 'd', a: 4, b: 4 },
{ n: 'e', a: 5, b: 5 } ]
> obj
{ c: { n: 'c', a: 3, b: 3 },
d: { n: 'd', a: 4, b: 4 },
e: { n: 'e', a: 5, b: 5 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment