Last active
January 14, 2022 06:59
-
-
Save khle/936a865ea5e325d01f28886f992a7a18 to your computer and use it in GitHub Desktop.
Step 6
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
interface User { | |
firstName: string | |
lastName: string | |
} | |
function findMatchingAndMoveToFirst( | |
users: readonly User[], | |
shouldBeFirst: User | |
) { | |
return users.reduce( | |
(accumulator: User[], current: User) => | |
current.firstName === shouldBeFirst.firstName && | |
current.lastName === shouldBeFirst.lastName | |
? [current, ...accumulator] | |
: [...accumulator, current], | |
[] | |
) | |
} | |
const users = [ | |
{ firstName: "Jane", lastName: "Foo" }, | |
{ firstName: "John", lastName: "Bar" }, | |
{ firstName: "Jill", lastName: "Err" }, | |
] | |
const reArrangedUsers = findMatchingAndMoveToFirst(users, { | |
firstName: "John", | |
lastName: "Bar", | |
}) | |
console.log(reArrangedUsers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment