Skip to content

Instantly share code, notes, and snippets.

@mjmeilahn
Created May 12, 2020 17:19
Show Gist options
  • Save mjmeilahn/37d346121d81cb6ec0f7588c055d4e9e to your computer and use it in GitHub Desktop.
Save mjmeilahn/37d346121d81cb6ec0f7588c055d4e9e to your computer and use it in GitHub Desktop.
JS: Object & Array Manipulation
// Assume a large array as the main argument with the following schema and manipulate accordingly.
// EXAMPLE SCHEMA:
// name: String
// weight: Number
// price: Number
// size: Number
// id: Number
// EXERCISE #1
// Return an Array of products priced greater than or equal to 75.
const highPricedProducts = products => {
return products.filter(product => product.price >= 75);
};
// EXERCISE #2
// Return an Array of products that are heavier than 1.8
const heavyProducts = products => {
return products.filter(product => product.weight > 1.8);
};
// EXERCISE #3
// Cast the Array into an Object where Keys become the ID's and the Values are the remaining Object Properties
// Keys should be iterable E.G. Object.keys(obj).map()
// Values should be searchable either numeric or string Keys E.G. obj[0], obj['0']
const arrayToObject = products => {
let obj = {};
for (let i = 0; i < products.length; i++) {
obj[i] = products[i];
}
return obj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment