0

Write a function that transforms some employee data from one format to another.

The argument will look something like this:

[  
  [  
    ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
  ], 
  [
    ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
  ]
]

Given that input, the return value should look like this:

 [ {firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
   {firstName: 'Mary', lastName: 'Jenkins', age: 36, role:'manager'}]

I know this isn't the most efficient code but I still cannot get the new array to contain each iteration of the object. I also don't necessarily want a new array. How do I take what is logged as "person" and put it into an array such as the example's output.

function keyValue (){
var arr3 = [];
var person = {}; 

for (var i = 0; i < arr.length; i++){
     for (var j = 0; j < arr[i].length; j++){
         var key = arr[i][j][0];
         var val = arr[i][j][1];
         person[key]  = val;
        }
    arr3[i] = person;
   }
  console.log(person) // desired output, but not returned
  return arr3;        // either only one instance "joe"  or 2
                      // "marys" depending on where I return
}
keyValue(arr)

 //console.logged()
    { firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk' }
{ firstName: 'Mary',
  lastName: 'Jenkins',
  age: 36,
  role: 'manager' }

 //returned
=> [ { firstName: 'Mary',
    lastName: 'Jenkins',
    age: 36,
    role: 'manager' },
  { firstName: 'Mary',
    lastName: 'Jenkins',
    age: 36,
    role: 'manager' } ]

1 Answers1

0

You need to create a person object for each array. Move var person = {} inside for loop, before second for loop

var arr = [
  [
    ['firstName', 'Joe'],
    ['lastName', 'Blow'],
    ['age', 42],
    ['role', 'clerk']
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['role', 'manager']
  ]
]

function keyValue(arr) {
  var arr3 = [];
  for (var i = 0; i < arr.length; i++) {
    var person = {};
    for (var j = 0; j < arr[i].length; j++) {
      var key = arr[i][j][0];
      var val = arr[i][j][1];
      person[key] = val;
    }
    arr3[i] = person;
  }
  return arr3; 
}

var res = keyValue(arr);
console.log(res);

You can alternatively use .map(), for..of loop

var arr = [
  [
    ['firstName', 'Joe'],
    ['lastName', 'Blow'],
    ['age', 42],
    ['role', 'clerk']
  ],
  [
    ['firstName', 'Mary'],
    ['lastName', 'Jenkins'],
    ['age', 36],
    ['role', 'manager']
  ]
];

var res = arr.map(array => {
  var obj = {};
  for (var [key, value] of array) {
    obj[key] = value
  }
  return obj
});

console.log(res);
guest271314
  • 1
  • 15
  • 104
  • 177
  • Thanks. What does "=>" do? – sopstem2428 Oct 26 '16 at 01:37
  • See [Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) , [Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?](http://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch) – guest271314 Oct 26 '16 at 01:51
  • Where did I go wrong on my code so that my logged and returned values were different? So I can understand my mistake, if you don't mind @guest271314 – sopstem2428 Oct 26 '16 at 03:43
  • @Tyler.Borer You need to create a `person` object for each array. Move `var person = {}` inside first `for` loop, before second `for` loop. See updated post. – guest271314 Oct 26 '16 at 03:54