This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Array - ar.find()</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div class="container py-5">
|
||||
<h1>Array - ar.find()</h1>
|
||||
<p>
|
||||
<a href="https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/find"
|
||||
>ar.find()
|
||||
</a>
|
||||
- Die Methode <strong>find()</strong> gibt den Wert des Elements eines Arrays zurück, welches
|
||||
<strong>als erstes</strong> die Bedingung einer bereitgestellten Testfunktion erfüllt. Andernfalls wird
|
||||
<strong>undefined</strong> zurückgegeben.
|
||||
</p>
|
||||
<div class="alert alert-warning">Side-effect-free</div>
|
||||
<div class="alert alert-secondary output"></div>
|
||||
</div>
|
||||
</main>
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const outputEl = document.querySelector('.output');
|
||||
|
||||
const winners = ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus'];
|
||||
|
||||
const names = [
|
||||
'Florian', //
|
||||
'Benjamin',
|
||||
'Christian',
|
||||
'Stefan',
|
||||
'Philippe',
|
||||
'Kaumon',
|
||||
];
|
||||
|
||||
// Array Methode - ar.find((currentValue [,index] [,array]) => {})
|
||||
|
||||
const foundName = names.find((name) => {
|
||||
return name.toLowerCase() === 'philippe';
|
||||
});
|
||||
|
||||
console.log(foundName); // => 'Philippe'
|
||||
|
||||
outputEl.innerHTML = `${foundName}`;
|
||||
|
||||
console.log(
|
||||
names.find((name) => {
|
||||
return name.toLowerCase() === 'Boris';
|
||||
}),
|
||||
); // => undefined <- bei keinem Treffer
|
||||
|
||||
// Optionale Parameter von Find ===================
|
||||
|
||||
// Array Methode - ar.find((currentValue, idx, arr) => {})
|
||||
names.find((name, idx, ar) => {
|
||||
console.log(`Name: ${name}, Index: ${idx}, Array: ${ar.join(', ')}`);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,73 @@
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Array - ar.findIndex()</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div class="container py-5">
|
||||
<h1>Array - ar.findIndex()</h1>
|
||||
<p>
|
||||
<a href="https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/findindex">
|
||||
ar.findIndex()
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
- Die Methode <strong>findIndex()</strong> gibt den Index des Elements eines Arrays zurück, welches
|
||||
<strong>als erstes</strong> die Bedingung einer bereitgestellten Testfunktion erfüllt. Andernfalls wird
|
||||
<strong>-1</strong> zurückgegeben.
|
||||
</p>
|
||||
<div class="alert alert-warning">Side-effect-free</div>
|
||||
<div class="alert alert-secondary output"></div>
|
||||
</div>
|
||||
</main>
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const outputEl = document.querySelector('.output');
|
||||
|
||||
const winners = ['Heribert', 'Friedlinde', 'Tusnelda', 'Oswine', 'Ladislaus'];
|
||||
|
||||
const names = [
|
||||
'Florian', //
|
||||
'Benjamin',
|
||||
'Christian',
|
||||
'Stefan',
|
||||
'Philippe',
|
||||
'Kaumon',
|
||||
];
|
||||
|
||||
// Array Methode - ar.findIndex((currentValue [,index] [,array]) => {})
|
||||
|
||||
const foundIdx = names.findIndex((name) => {
|
||||
return name.toLowerCase() === 'philippe';
|
||||
});
|
||||
|
||||
const foundIdx2 = names.indexOf('Philippe');
|
||||
|
||||
console.log(foundIdx); // => 4
|
||||
console.log(foundIdx2); // => 4
|
||||
|
||||
outputEl.innerHTML = `${foundIdx}`;
|
||||
|
||||
console.log(
|
||||
names.findIndex((name) => {
|
||||
return name.toLowerCase() === 'Boris';
|
||||
}),
|
||||
); // => -1 <- bei keinem Treffer
|
||||
|
||||
console.log(names.indexOf('Boris')); // => -1
|
||||
|
||||
// Optionale Parameter von findIndex ===================
|
||||
|
||||
// Array Methode - ar.findIndex((currentValue, idx, arr) => {})
|
||||
names.findIndex((name, idx, ar) => {
|
||||
console.log(`Name: ${name}, Index: ${idx}, Array: ${ar.join(', ')}`);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user