how do I convert an object to an array list in JS

Salma

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);

m0meni

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.

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i convert a json array object of an object into a java 8 optional list of that object

How do I convert array of Objects into one Object in JavaScript?

How do I convert an object array to an object list

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert an array object to a string in PowerShell?

How do I convert an object to an array?

How do I convert array of objects to object in javascript?

How do I convert an array of strings to an object property in javascript?

How do you convert a List of object to a one dimensional array?

How do i convert an array with varying size to a JSON object?

How do i convert List into datetime.date object?

How do I convert a Json file (that contains a array) to a List (of a class object) C# Unity

How do I convert array of Objects to one Object in JavaScript?

how do i convert json object to array without key

How do i convert my values in an array/object to measure them?

How do I convert string array values to object in c#

How do I convert an Array with a JSON string into a JSON object (ruby)

How do I convert List<List<Object>> to a multi-dimensional array?

How do I use GSON to convert an object array to an array of their IDs

How do I convert an array into an object within array - Angular and Javascript

How do I convert Pandas object to a list in python3

How can I convert this list of pairs into an object that contains an array of arrays?

Error 'int' object is not subscriptable, How do i convert it to list?

How do I convert an array or a list into a hashref?

How do I Convert an array of object into single object after grouping in js?

How do I convert an array object into a two-dimensional array

How do I convert a list into a dictionary grouped by object name?

How do i convert JSON object to an array as above?

How do i convert an array of objects into 1 object in typescript