Skip to content

Instantly share code, notes, and snippets.

@hmmhmmhm
Created September 17, 2021 02:50
Show Gist options
  • Select an option

  • Save hmmhmmhm/8641c6c49252119177f24c8781195de9 to your computer and use it in GitHub Desktop.

Select an option

Save hmmhmmhm/8641c6c49252119177f24c8781195de9 to your computer and use it in GitHub Desktop.
Check Map For Some
const list = [{ text: "a" }, { text: "b" }, { text: "c" }, { text: "d" }];
// Map & Filter
const i1 = list
.map((item, index) => (item.text === "c" ? index : null))
.filter((item) => item !== null)[0]!;
// For
let i2 = 0;
for (const item of list) {
if (item.text === "c") {
i2 = list.indexOf(item);
break;
}
}
// Some
let i3 = 0;
list.some((item, index) => {
if (item.text == "b") {
i3 = index;
return true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment