0

Trying to manipulate this:

input = [ 
[ ['a','b'], ['c','d'], ['e','f'] ], 
[ ['g','h'], ['i','j'],  ]
]

to

output = [
    {a: 'b', c: 'd', e: 'f'},
    {g: 'h', i: 'j'},
]

Here's what I have so far:

function transform(array) {
  result = [];
  for (var i=0; i<array.length; i++){
    for (var j=0; j<array[i].length; j++){

    // How can I create an object here? 

    object.array[i][j][0] = array[i][j][1];
    }
  }
return result; 
}

I'm trying to solve this as a coding challenge so I don't necessarily want the answer but I'm unsure of how to proceed. Since the number of arrays that have a pair of strings inside is not uniform (for instance, first set of arrays within the input array has 3 sets, and the second set has 2 sets, I reckon I need to dynamically create objects within each loop that I can add to the results array at the end. How can I do this?

I don't think I'm supposed to use any sort of fancier / higher order functions. The goal is to build up my familiarity with the fundamentals.

Faique Moqeet
  • 569
  • 1
  • 5
  • 10
  • `var obj = {}; for (...) { obj[key] = value; }`. See [Is it possible to add dynamically named properties to JavaScript object?](http://stackoverflow.com/q/1184123/218196) – Felix Kling Feb 16 '17 at 04:33
  • I’d recommend you work on a function that just does one transformation so the nested loops don’t get in the way of understanding. Write one that just transforms `[['key1', 'value1'], ['key2', 'value2']]` to `{key1: 'value1', key2: 'value2'}`. https://stackoverflow.com/questions/695050/how-do-i-add-a-property-to-a-javascript-object-using-a-variable-as-the-name might help. – Ry- Feb 16 '17 at 04:33
  • @FelixKling How does that solve the issue since I need to be able to create these objects x amount of times and x is varied? – Faique Moqeet Feb 16 '17 at 04:35
  • The whole point of a loop is to write code once and have it be executed x times. Not sure where the confusion is? – Felix Kling Feb 16 '17 at 04:36

3 Answers3

1

You can use reduce to process the outer and inner arrays, e.g.

var input = [
             [['a','b'], ['c','d'],['e','f'] ],
             [['g','h'], ['i','j'],]
            ];

// For each outer array
var result = input.reduce(function(acc, a){

  // Create an object from the inner arrays
  acc.push(a.reduce(function(acc, a) {
    acc[a[0]] = a[1];
    return acc;
  },{}));
  return acc;
}, []);

console.log('With reduce\n');
console.log(result);

// Same algorithm using for loops:
var result2 = [];

// For each outer array
for (var i=0, iLen=input.length; i<iLen; i++) {
  var acc = {};
  var a = input[i];

  // Loop over the inner arrays to build an object, 
  // then push into result array
  for (var j=0, jLen=a.length; j<jLen; j++) {
    var b = a[j]
    acc[b[0]] = b[1];
  }
  result2.push(acc);
}
console.log('\nWith loops')
console.log(result2);
RobG
  • 142,382
  • 31
  • 172
  • 209
0
Input.reduce((memo,num) => {
    memo[num[0]]=num[1];
    return memo;
},{})
Aaroh Mankad
  • 176
  • 5
  • 1
    Code only answers are not that useful. Please *explain* your solution so that others can learn. I'm sure the OP doesn't know what `reduce` is. – Felix Kling Feb 16 '17 at 04:39
  • 1
    This answer has the same issue as Ling Qing Meng's. – RobG Feb 16 '17 at 04:46
0

You can use nested for..of loops to iterate each inner array, create object, set property to element at index 0, value to element at index 1, push object to array at completion of nested for..of loop

let input = [
  [
    ['a', 'b'],
    ['c', 'd'],
    ['e', 'f']
  ],
  [
    ['g', 'h'],
    ['i', 'j']
  ]
];

let output = [];

for (let arr of input) {
  let o = {};
  for (let [key, value] of arr) {
    o[key] = value;
  }
  output.push(o);
}

console.log(output);
guest271314
  • 1
  • 15
  • 104
  • 177