1

I have a problem with my code it has a running error I am trying to convert an object to an array list. In my code, I was trying to build an array inside an array! I was expecting to have a return value as the following:

[['name', 'Holly'], ['age' , 35], ['role', 'producer' ],

['species', 'canine'], ['name', 'Bowser'], ['weight', 45]] 

I will appreciate any help or advice thanks.

var obj1 = {
  name: 'Holly',
  age: 35,
  role: 'producer'
};
var obj2 = {
  species: 'canine',
  name: 'Bowser',
  weight: '45'
};

function convertObjectToList(obj) {
  var arrayExt = []; // declare my external array container
  var arrayInt = []; // declare my internal arrays container
  var objKeys = Object.getOwnPropertyNames(obj); // returns all properties (enumerable or not) found directly upon a given object. 
  for (var k in obj) {
    if (var i = 0; i < objKeys.length; i++);
    arrayInt = [];
    arrayInt.push(obj(objKeys[k]));
    arrayExt.push(arrayInt);
    console.log(arrayExt);
  }
}
convertObjectToList(obj);

2
  • 2
    It sounds like you want Object.entries. It has low browser support, but there are polyfills. Commented Jul 14, 2017 at 0:12
  • What is if (var i = 0; i < objKeys.length; i++); supposed to be? You're mixing up if and for syntax. Commented Jul 14, 2017 at 0:42

3 Answers 3

3

This should do the trick?

    var obj1 = { name: 'Holly', age: 35, role: 'producer' };
    var obj2 = { species: 'canine', name: 'Bowser', weight: '45'};

    function convertObj(obj) {
        var result = [];
        for (var key in obj) {
            result.push([key, obj[key]])
        }
        return result;
    }
    
    console.log(convertObj(obj1))
    console.log(convertObj(obj2))

Gives (NodeJS):

> convertObj(obj1)
[ [ 'name', 'Holly' ],
  [ 'age', 35 ],
  [ 'role', 'producer' ] ]
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use a use a combination of Object.keys, Array.map, and Array.concat

function toArray(obj) {
  return Object.keys(obj).map(k => [k, obj[k]]);
}

var obj1 = { name: 'Holly', age: 35, role: 'producer' };
var obj2 = { species: 'canine', name: 'Bowser', weight: '45'};

console.log(toArray(obj1).concat(toArray(obj2)))

In general, you should avoid for...in loops. There are many better alternatives now.

2 Comments

Thanks@AR7 really helped me
@Salma let me know if you have any followup questions or if anything wasn't clear
1

With ECMAScript2017 you can use Object.entries or a traditional "for in" loop

var obj1 = {
  name: 'Holly',
  age: 35,
  role: 'producer'
};
var obj2 = {
  species: 'canine',
  name: 'Bowser',
  weight: '45'
};



var arr1 = Object.entries(obj1)

console.log(arr1);

var arr2 = Object.entries(obj2)

console.log(arr2);



//traditional for in loop

var tr1 = [];
for (let iter in obj1) {
  tr1.push([iter, obj1[iter]])

}

console.log(tr1);

var tr2 = [];
for (let iter in obj2) {
  tr2.push([iter, obj2[iter]])

}
console.log(tr2);

5 Comments

NodeJS: TypeError: Object function Object() { [native code] } has no method 'entries'
@Fabien upgrade your version of Node
Damned, maybe :-D Another approach is to use Object.keys(a).map(function(keyName, keyIndex) { ...
this gives the result but gives an undefined
@salma what can you explain what you mean by undefined? are you getting this in Node?

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.