Skip to content

Instantly share code, notes, and snippets.

@jmahc
Created January 20, 2017 18:15
Show Gist options
  • Save jmahc/898033e2e24b031770210d3b803110cc to your computer and use it in GitHub Desktop.
Save jmahc/898033e2e24b031770210d3b803110cc to your computer and use it in GitHub Desktop.
Create object with keys from an array of strings and values that are an array containing a specific string from the `key array`.
/*
* -----------------------------------------------------------------------------------------
* ==== End goal is to have an object, `myObject` look like this: =========================
* -----------------------------------------------------------------------------------------
* myObject: {
* 'firstKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-firstKey'],
* 'secondKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-secondKey'],
* 'thirdKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-thirdKey']
* }
*/
const myObject = {};
const objectKeyNamesArray = ['firstKey', 'secondKey', 'thirdKey'];
const objectValuesArray = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
objectKeyNamesArray.map(function(arrayValue) {
// ==== Copy of `objectValuesArray`.
// ** Purpose: Allows for the manipulation of values in `objectValuesArray`
// without affecting the original array or other key-values.
// !! `.slice(0)` allows us to create the copy.
// ----
// Current State (first iteration)
// >> myObject = {};
// >> objectValuesArray = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
// >> copy = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
const copy = objectValuesArray.slice(0);
// ==== Custom value dependent on `arrayValue`.
// ----
// Current State (first iteration)
// >> myObject = {};
// >> objectValuesArray = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
// >> copy = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
// >> custom = 'value-firstKey';
const custom = 'value-' + arrayValue;
// ==== Insert, or `push`, the `custom` value into `copy` array.
// ----
// Current State (first iteration)
// >> myObject = {};
// >> objectValuesArray = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
// >> copy = ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-firstKey'];
// >> custom = 'value-firstKey';
copy.push(custom);
// ==== Create a key for `myObject` using the current `arrayValue` with a value of `copy`
// ----
// Current State (first iteration)
// >> myObject = {
// 'firstKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-firstKey']
// };
// >> objectValuesArray = ['valueOne', 'valueTwo', 'valueThree', 'valueFour'];
// >> copy = ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-firstKey'];
// >> custom = 'value-firstKey';
myObject[arrayValue] = copy;
});
// ==== Check the output
// + Actual ouput is our desired ouput! :)
/*
* myObject: {
* 'firstKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-firstKey'],
* 'secondKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-secondKey'],
* 'thirdKey': ['valueOne', 'valueTwo', 'valueThree', 'valueFour', 'value-thirdKey']
* }
*/
console.log(myObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment