-9

I have been tasked to convert the following array to an array of object pairs:

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

I need to transform arr into an array of objects that looks like this:

[
 {firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
 {firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]
4
  • 4
    Please put what effort you have taken and what output you want Commented Nov 11, 2016 at 7:08
  • What is the output format? Commented Nov 11, 2016 at 7:09
  • 1
    You want like this - var arr = [{'firstName':'Joe','lastName':'Blow','age':42,'role':'clert'},{'firstName':'Mary','lastName':'Jenkins','age':36,'role':'manager'}] Commented Nov 11, 2016 at 7:09
  • Mention what you have tried, what's the output desired. Also, format the question, add code blocks. The arr is missing a ']' Commented Nov 11, 2016 at 7:11

6 Answers 6

3

Programmatic solution (available too at https://jsfiddle.net/edorka/jnLxyzhb/):

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

var empty = {};
var result = arr.map(function(objectArray){
    var object = this;
    var attribute = objectArray.map(function(attrArray){
        var name = attrArray[0], value = attrArray[1];
        object[name] = value;
        return object;
    }, object);
    return this;
}, empty);

console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

0

You can convert it like this -

var arr = [
            {'firstName':'Joe','lastName':'Blow','age':42,'role':'clert‌​'},
            {'firstName':'Mar‌​y','lastName':'Jenki‌​ns','age':36,'role':‌​'manager'}
          ]

1 Comment

what's the difference between this and what they posted? how does this help them achieve that? this just seems like a reiteration of what they said they wanted, plus some quotes
0

example of an array of objects

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

Comments

0

You could use an iterative and recursive approach for the nested arrays.

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

array[0].forEach(function iter(a, i) {
    i || result.push({});
    if (i < 4) {
        result[result.length - 1][a[0]] = a[1];
        return;
    }
    Array.isArray(a) && a.forEach(iter);
});
   
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

5 Comments

Didn't knew about embedded snippets, thanks for your response. However IMHO I see your solution limited by the number of attributes (four) but that would lack elasticity, I mean, if i == 2 then it's a name/value tuple.
i used the data i have, and it looks like the first four elements are meant to be part of the object.
That's what I was able to come up with
It does work but I don't think it is the most efficient solution.
if you supply more data, than above, i could optimize the proposal.
0
function convert(array) {
  return array.map(function(person) {
    return person.reduce(function(obj, prop, index) {
      if (!obj[prop[0]]) {
        obj[prop[0]] = prop[1];
      }

      return obj;
    }, {});
  }); 
}
  • use the map function to select each "person" array
  • use the reduce function to convert each "person" array to an object
  • the map function will also return a new array of objects
  • the original array will not be altered

Comments

-1

If you format you your code you will see at first there is 1 closing brace ']' at the end is missing. And you structure is like this

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

I think you want to do something like this

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

But i will suggest you to do like this

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

1 Comment

Please drop the comment with that negative vote. So i can get where i was wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.