Skip to content

Instantly share code, notes, and snippets.

@michaelmcshinsky
Last active June 17, 2020 12:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save michaelmcshinsky/bf25d3eac40ef4472bee6e5ac87ba537 to your computer and use it in GitHub Desktop.
let nonPrimitiveArray = [
{name: 'Alpha', letter: 'A'},
{name: 'Bravo', letter: 'B'},
{name: 'Charlie', letter: 'C'}
];
// Objectives:
// 1. Find the value Bravo
// 2. Find the index of 1
const OBJECTIVE_STRING = 'Bravo';
let arr = [
{name: 'Alpha', letter: 'A'},
{name: 'Bravo', letter: 'B'},
{name: 'Charlie', letter: 'C'},
{name: 'Delta', letter: 'D'},
{name: 'Echo', letter: 'E'},
{name: 'Foxtrot', letter: 'F'},
{name: 'Golf', letter: 'G'},
];
let foundObject = null;
let foundIndex = -1;
// Using array and variables from base code block above… (starter-code.js)
for (let index = 0; index < arr.length; index++) {
const value = arr[index];
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
foundIndex = index;
break;
}
};
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
for (const value of arr) {
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
}
};
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
// Using array and variables from base code block above… (starter-code.js)
arr.forEach((value, index) => {
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
foundIndex = index;
}
});
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
let length = arr.length;
let index = 0;
while(index < length) {
const value = arr[index];
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
foundIndex = index;
index = length - 1;
}
index++;
};
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
let length = arr.length;
let index = 0;
do {
const value = arr[index];
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
foundIndex = index;
index = length - 1;
}
index++;
} while (index < length);
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
foundObject = arr.find((value, index) => {
if(value.name === OBJECTIVE_STRING) {
foundObject = index;
return true;
}
return false;
});
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
foundIndex = arr.findIndex((value, index) => {
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
return true;
}
return false;
});
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
foundIndex = arr.lastIndexOf((value, index) => {
if(value.name === OBJECTIVE_STRING) {
foundObject = value;
return true;
}
return false;
});
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
// This an anti-pattern when used ONLY to loop and find a value/index rather than the intended function of returning a new array.
arr.map((value, index) => {
if(value.name === OBJECTIVE_STRING && foundIndex !== -1) {
foundObject = value;
foundIndex = index;
}
});
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// reduce
// Using array and variables from base code block above… (starter-code.js)
// This an anti-pattern when used ONLY to loop and find a value/index rather than the intended function of returning a value.
foundObject = arr.filter((value, index) => {
if(value.name === OBJECTIVE_STRING && foundIndex !== -1) {
foundIndex = index;
}
})[0];
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
// Using array and variables from base code block above… (starter-code.js)
// This an anti-pattern when used ONLY to loop and find a value/index rather than the intended function of returning a new value.
arr.reduce((accumulator, current, index) => {
if(current.name === OBJECTIVE_STRING && foundIndex !== -1) {
foundObject = current;
foundIndex = index;
}
return accumulator;
});
console.log(foundObject); // expected output: {name: 'Bravo', letter: 'B'};
console.log(foundIndex); // expected output: 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment