Last active
February 15, 2018 18:59
-
-
Save fmarcia/97e6c61291032eaeb9116c28f36b14d6 to your computer and use it in GitHub Desktop.
Remove items
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
var limit = 4; | |
arr = arr.filter(e => { | |
if (e.a < limit) { | |
delete obj[e.n]; | |
} else { | |
return true; | |
} | |
}); |
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
// 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); |
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
> 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