Created
August 28, 2017 18:56
-
-
Save feload/2494784ec9842c303b95da9f0c674a02 to your computer and use it in GitHub Desktop.
toTop.js Send certain objects to the top of the stack.
This file contains hidden or 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
/** | |
* toTop() | |
* Send certain objects to top of the stack. | |
*/ | |
const toTop = (col, what, where) => { | |
const ttop = []; | |
const tail = []; | |
col.forEach((i) => { | |
if(i[where] == what){ | |
ttop.push(i); | |
}else{ | |
tail.push(i); | |
} | |
}); | |
return ttop.concat(tail); | |
}; | |
/** | |
Example: | |
const col = [ | |
{ l: 'a', p: 1 }, | |
{ l: 'e', p: 2 }, | |
{ l: 'i', p: 4 }, | |
{ l: 'g', p: 3 }, | |
{ l: 'b', p: 1 }, | |
{ l: 'f', p: 2 }, | |
{ l: 'c', p: 1 }, | |
{ l: 'i', p: 4 }, | |
{ l: 'h', p: 3 }, | |
{ l: 'd', p: 1 } | |
]; | |
const what = 1; // Value. | |
const where = 'p'; // Property. | |
console.log(toTop(col, what, where)); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment