0

I'm not sure what I am doing wrong in this. When I try to run the code, it says that there is an undefined here: obj[array[i]array[j][0]] = obj[array[i]array[j][1]];

Can someone explain what I am doing incorrect? I am looking for to produce an object return that looks like

obj = {
    firstName:'Joe'
}



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

function transformEmployeeData(array){    
    var obj = {};    
    for(var i = 0; i < array.length; i++){    
        for(var j = 0; j < array[i].length; j++){    
            obj[array[i]array[j][0]] = obj[array[i]array[j][1]];    
        }    
    }    
    return obj;    
}      
transformEmployeeData(array);
2
  • Seems to be an exact duplicate of this Commented Jan 22, 2017 at 19:39
  • @Redu - Agreed. Looks like it could be homework. Commented Jan 24, 2017 at 4:47

5 Answers 5

2

A simple way is to use map to create the array of objects and forEach to assign each name/value pair.

   var array = [
     [
       ['firstName', 'Joe'],
       ['lastName', 'Blow'],
       ['age', 42],
       ['role', 'clerk']
     ],
     [
       ['firstName', 'Mary'],
       ['lastName', 'Jenkins'],
       ['age', 36],
       ['role', 'manager']
     ]
   ]
   
   function transformEmployeeData(array){
     return array.map(a =>{ 
       var obj = {}; 
       a.forEach(a => obj[a[0]] = a[1]); 
       return obj;
     });
   }
   var results = transformEmployeeData(array);
   console.log(results[0]);
   console.log(results[1]);

or a more compact form

function transformEmployeeData(arr){
  var obj; 
  return arr.map(a => (a.forEach((a, i) => (obj = !i ? {} : obj, obj[a[0]] = a[1])), obj));
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need an array less and no object for the second part.

obj[array[i][j][0]] = array[i][j][1];

And you need an array for collecting the temporary object. This has to be returned.

function transformEmployeeData(array) {
    var result = [],
        obj;

    for (var i = 0; i < array.length; i++) {
        obj = {};
        for (var j = 0; j < array[i].length; j++) {
            obj[array[i][j][0]] = array[i][j][1];
        }
        result.push(obj);
    }
    return result;
}

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

console.log(transformEmployeeData(array));

2 Comments

Thanks Nina, can you explain why I don't need to write array[j]?
you get a non named array back, not a variable.
0

Why would you put data into an array? Where's the object?

I would expect to see an Employee object in a List:

public class Employee {
    private final String firstName;
    private final String lastName;
    private final LocalDate birthDate;
    private final String role;

    public Employee(String f, String n, LocalDate b, String r) {
        this.firstName = f;
        this.lastName = n;
        this.birthDate = LocalDate.from(b);
    }
    // add the rest
}

Comments

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

function transformEmployeeData(arr){
  var result = {};
       arr.forEach(function(subArr, i){
         result[i] = {};
         subArr.forEach(function(item){
           result[i][item[0]] = item[1];
         });
       });

  console.log(result);
}      
transformEmployeeData(array);

https://plnkr.co/edit/8ZQTsC3SKYTyEvMlrONt?p=preview

Comments

0

You want an array of objects as follows:

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

function transformEmployeeData(array){    
    var obj = [];    
    for(var i = 0; i < array.length; i++){
        obj.push({});
        for(var j = 0; j < array[i].length; j++){    
            obj[obj.length - 1][array[i][j][0]] = array[i][j][1];
        }    
    }    
    return obj;    
}   
var obj = transformEmployeeData(array);
var result = JSON.stringify(obj);
console.log(result);

The output

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

The reason why obj[array[i]array[j][0]] = obj[array[i]array[j][1]]; does not work is two fold:

1) You want to assign to your new object from array not from obj which doesn't exist yet.

2) array[i]array[j][0] is not a valid reference into your array. Instead of array[j] you want to just use [j]

For example: for i=0, j=0, you are saying array[0]array[0][0] but you really want array[0][0][0]. array[0]array[0] is not a valid syntax (array[0][array[0]][0] is but it is not a correct reference).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.