javascript dynamically populate associative array and get values

Viewed 31622

I want to build an associative array based on an array and then get the values of that associative array. The structure of the associative array is as follows:

        var myAssociativeArr = new Array();

        myAssociativeArr = [
         { id:'1',
          lname:'doe',
          fname:'john' 
        },
         { id:'2',
          lname:'smith',
          fname:'john' 
        }

        ]

I have three arrays of the same length from which I will build this associative array i.e. id, lname and fname array.

    for(var i=0; idArray.length;i++)
    {
    myAssociativeArr [id]=idArray[i];
    myAssociativeArr [lname]=lnameArray[i];
    myAssociativeArr [fname]=fnameArray[i];
    }

Please tell me if the above approach is correct, if not how can I build this array and also how can I get the values of this array via a loop.

Your help will be appreciated.

4 Answers

Fast-forward, eight years later, using ECMAScript 2015, we would solve this problem by doing:

let assocArr = idArray
.map(x=>{ return {id: x}})
.map((x,index) => Object.assign({lname: lname[index]}, x))
.map((x,index) =>Object.assign({fname: fname[index]}, x) )
.reduce((assoc,obj) => {let target = {};target[obj.id] = obj; return Object.assign(target,assoc) }, {})

Or by using reduce only:

let assocArr = idArray.reduce((assoc, x, index) => {
  let target = {};
  target[x] = { id: x, lname: lname[index], fname: fname[index] };
  return Object.assign(target, assoc);
}, {});

If it were ever needed, a more flexible function that does the kind of things above is:

function glue(properties) {
  let propsArrays = Object.entries(properties);
  let lookUp = properties[properties.lookUpKey];
  let propertyArrays = propsArrays.filter(
    ([key, value]) => key !== "lookUpKey"
  );
  return propertyArrays.reduce(
    (assoc, props, index) => {
      let [key, values] = [...props];
      return values.reduce((assoc, prop, j) => {
        let target = {};
        target[key] = prop;
        assoc[lookUp[j]] = Object.assign(target, assoc[lookUp[j]]);
        return assoc;
      }, assoc);
    },
    lookUp.reduce((assoc, id) => {
      assoc[id] = {};
      return assoc;
    }, {})
  );
}

You give it an object such as

var properties = {id: idArray, lname: lname, fname: fname, lookUpKey: 'id'}

And based on the object's property names as well as the lookup array key, and it will glue those properties together as objects in an array. The function turns the properties object into an array of key/value pairs with:

let propsArrays = Object.entries(properties);

Then it extracts the lookUp keys and the actual properties to glue into their respective arrays:

let lookUp = properties[properties.lookUpKey];
let propertyArrays = propsArrays.filter(([key, value]) => key !== "lookUpKey");  

Then, it uses nested reduces to process the properties into arrays of objects with the right properties. Note that:

lookUp.reduce((assoc, id) => {
          assoc[id] = {};
          return assoc;
        }, {})

initializes the outermost reduce with an object with keys/properties that are the values in the lookup array. The values associated to those keys are just empty objects at the beginning. The innermost reduce will then merge the appropriate properties into those objects by using:

assoc[lookUp[j]] = Object.assign(target, assoc[lookUp[j]])
Related