28
loading...
This website collects cookies to deliver better user experience
const users = [
{
firstName: "Amy",
lastName: "Effertz",
id: "5778c2ac-b82f-45e6-8aa3-0b6d83e9a6bb",
isActive: false,
},
{
firstName: "Chaim",
lastName: "Halvorson",
id: "248a9de0-d8e8-4f8e-ac64-311185b47168",
isActive: true,
},
{
firstName: "Elyssa",
lastName: "Konopelski",
id: "f0917603-06fb-45d8-befc-e716319122b3",
isActive: true,
},
{
firstName: "Kendall",
lastName: "Glover",
id: "aae249e6-e36b-4889-a982-6babe17dd696",
isActive: false,
},
{
firstName: "Sigmund",
lastName: "Eichmann",
id: "f3505b2a-7e85-4994-b3c6-3b62a4b7f77c",
isActive: false,
}
];
function getOneActiveAndOneInactiveUser() {
const active = users.find((user) => user.isActive);
const inactive = users.find((user) => !user.isActive);
return [active, inactive].filter((user) => typeof user !== "undefined");
}
find
works.function getFirstOppositeOccurences() {
let temp = {};
for (let i = 0; i < users.length; i++) {
if (users[i].isActive) {
if (typeof temp[0] === "undefined") {
temp[0] = users[i];
} else {
continue;
}
} else {
if (typeof temp[1] === "undefined") {
temp[1] = users[i];
} else {
continue;
}
}
}
return Object.values(temp);
}
for
like it's... 1999, it has a variable named temp
like we are writting code in some kind of university, it has a lot of "if elses" and it still depends on the users
and their isActive
property. The only "cool" thing was the use of Object.values
to create that array.isActive
condition. I needed to add my active user first and inactive second. A condition always returns a boolean so I immediately thought of casting that boolean to a number:+true; // 1
+false; // 0
+!true; // 0
+!false; // 1
if
was gone and the second iteration of the function looked like this.function getFirstOppositeOccurences2() {
let temp = {};
for (let i = 0; i < users.length; i++) {
const index = +!users[i].isActive;
if (typeof temp[index] === "undefined") {
temp[index] = users[i];
} else {
continue;
}
}
return Object.values(temp);
}
if
was simply checking to add a user entry only if it was not already merged to the temp
object.if
by turning your if
to something like this.for (let i = 0; i < users.length; i++) {
const index = +!users[i].isActive;
temp = { ...temp, ...(!temp[index] && { [index]: users[i] }) };
}
if
but it will work as a guard clause.function getFirstOppositeOccurences() {
let temp = {};
for (let i = 0; i < users.length; i++) {
const index = +!users[i].isActive;
if (typeof temp[index] !== "undefined") continue;
temp[index] = users[i];
}
return Object.values(temp);
}
function getFirstOppositeOccurences(array) {
let temp = {};
for (let i = 0; i < array.length; i++) {
const index = +!array[i].isActive;
if (typeof temp[index] !== "undefined") continue;
temp[index] = array[i];
}
return Object.values(temp);
}
+!array[i].isActive;
isActive
property.array[i]
find
, map
etc.users.find((user) => user.isActive)
function getFirstOppositeOccurences(array, callbackFn) {
let temp = {};
for (let i = 0; i < array.length; i++) {
const index = +!callbackFn(array[i], i, array);
if (typeof temp[index] !== "undefined") continue;
temp[index] = array[i];
}
return Object.values(temp);
}
+!callbackFn(array[i], i, array)
// get two users, one active and one inactive
getFirstOppositeOccurences(users, (user) => user.isActive);
// get two users, the third one (index === 2) and one that is not the third one
getFirstOppositeOccurences(users, (user, index) => index === 2);
if (array.length === 0) return [];
if (typeof array === "undefined") return [];
if (typeof callbackFn === "undefined") return [];