Convert nested array into array of objects problem

array of objects javascript
convert nested objects to array javascript
how to access array of objects in javascript
javascript multidimensional array
javascript multidimensional object
convert a complex array to an object
javascript array
javascript multidimensional array push

I'm trying to convert this nested array data into an array of objects. Here is the data.

  const tableData = [
    ["first_name", "last_name", "city", "state"],
    ["June", "Gregory", "Hammond", "IN"],
    ["James", "Wynn", "Gary", "IN"],
    ["Craig", "Norman", "Schererville", "IN"]
  ]

Here is what the output should be

[
  { first_name : "June", last_name : "Gregory", city: "Hammond", state : "IN" },
  { first_name : "James", last_name : "Wynn", city: "Gary", state : "IN" },
  { first_name : "Craig", last_name : "Norman", city: "Schererville", state : "IN" }
]

Here is my code.

    function convertTable(table_data) {
var result = []
var key, value
for (var i=0; i<table_data.length; i++) {
  var employee = {}
  for (var j=0; j<table_data[i].length; j++) {
    key = table_data[i][j][0]
    value = table_data[i][j][1]
    employee[key] = value
   }
   result.push(employee)
  }
  return result
 }

  var input = [
    ["first_name", "last_name", "city", "state"],
    ["June", "Gregory", "Hammond", "IN"],
    ["James", "Wynn", "Gary", "IN"],
    ["Craig", "Norman", "Schererville", "IN"]
  ]

console.log(convertTable(input));

The solution is printing out the first letters of the words instead of the whole word. I am having a hard time understanding why. I ask if any commenters could leave comments line by line to help aid my understanding of this program. I wrote it with assistance of researching similar problems but still having a hard time understanding. I have been reading about Javascript on w3schools, MDN, and watching Youtube videos on explanations. Any other help would be appreciated, thanks.

Try this,

You just misplaced some index except that everything is fine. Just match with your function and you can find easily where you missed.

  const tableData = [
    ["first_name", "last_name", "city", "state"],
    ["June", "Gregory", "Hammond", "IN"],
    ["James", "Wynn", "Gary", "IN"],
    ["Craig", "Norman", "Schererville", "IN"]
  ]


    function convertTable(table_data) {
var result = []
var key, value
for (var i=1; i<table_data.length; i++) {
  var employee = {}
  for (var j=0; j<table_data[i].length; j++) {
    key = table_data[0][j]
    value = table_data[i][j]
    employee[key] = value
   }
   result.push(employee)
  }
  return result
 }

  var input = [
    ["first_name", "last_name", "city", "state"],
    ["June", "Gregory", "Hammond", "IN"],
    ["James", "Wynn", "Gary", "IN"],
    ["Craig", "Norman", "Schererville", "IN"]
  ]

console.log(convertTable(input));
  { first_name : "June", last_name : "Gregory", city: "Hammond", state : "IN" },
  { first_name : "James", last_name : "Wynn", city: "Gary", state : "IN" },
  { first_name : "Craig", last_name : "Norman", city: "Schererville", state : "IN" }
] */

How To Convert Objects to Arrays and Vice Versa in JavaScript, entries() method from the Object class. This will convert our object to an array of arrays. Each nested array is a two-value list where the first item is  Im trying to convert this nested array data into an array of objects Here is the data const tableData firstname lastname city state June Gregory Hammond IN James Wynn

First of all, you took for both values a single character, which is not necessary,

key = table_data[i][j][0]
//                    ^^^
value = table_data[i][j][1]
//                      ^^^

then you need two changes, one for the outer loop to omit the keys and inside use the first row for the keys. Please see in the code the comments.

The other change is to declare all variable at top of the function.

function convertTable(table_data) {
    var result = [],
        key, value,
        employee,
        i, j;

    for (i = 1; i < table_data.length; i++) {        // start from index 1
        employee = {};
        for (j = 0; j < table_data[i].length; j++) {
            key = table_data[0][j];                  // take the value form index zero
                                                     // no following index for the letter
            value = table_data[i][j];                // no following index for the letter
            employee[key] = value;
        }
        result.push(employee);
    }
    return result;
}

var input = [["first_name", "last_name", "city", "state"], ["June", "Gregory", "Hammond", "IN"], ["James", "Wynn", "Gary", "IN"], ["Craig", "Norman", "Schererville", "IN"]]

console.log(convertTable(input));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Convert nested array into array of objects problem, I'm trying to convert this nested array data into an array of objects. Here is the data. const tableData = [ ["first_name", "last_name", "city", "state"], ["June",  Note: Quick Select does not select or count blocks nested in associative array objects. Modify the Items in an Associative Array With associative arrays, you can perform edits directly using grips, with grip menu options that appear when you hover over a grip, or by using the ARRAYEDIT command from the contextual tab or at the Command prompt.

Here is a concise version using Array.reduce:

const data = [["first_name", "last_name", "city", "state"], ["June", "Gregory", "Hammond", "IN"], ["James", "Wynn", "Gary", "IN"], ["Craig", "Norman", "Schererville", "IN"]]

const result = data.reduce((r,c,i,arr) => {
  if(i) r.push(c.reduce((a,b,j) => (a[arr[0][j]] = b, a), {}))
  return r
}, [])
console.log(result)

Data Structures: Objects and Arrays :: Eloquent JavaScript, Turning into a squirrel does cause fewer problems than turning into a wolf. You give it an object, and it returns an array of strings—the object's property names. A list is a nested set of objects, with the first object holding a reference to the  Now I got to try it and it does convert the List into an Array but you cannot call for example map on the array since the objects inside are still lazy Realm Objects. One way that works is to call the lodash collection functions _.map(array, ..). It would be great if there was a function that would go deep into every object property and

You was accessing a 3-level deep on the root array, so you where indexing the strings inside the 2-level arrays.

Using this: key = table_data[i][j][0] you was accessing the first character of the j-placed string inside the array on index i.

And using this: value = table_data[i][j][1] you was accessing the second character of the j-placed string inside the array on index i.

Also, you already know the first array are the keys (or headers), so you don't want to loop on it. Check next example:

function convertTable(table_data)
{
    var result = [];
    var key, value;
    
    for (var i = 1; i < table_data.length; i++)
    {
        var employee = {};

        for (var j = 0; j < table_data[i].length; j++)
        {
            key = table_data[0][j];
            value = table_data[i][j];
            employee[key] = value;
        }

        result.push(employee);
     }
     
     return result;
 }

  var input = [
    ["first_name", "last_name", "city", "state"],
    ["June", "Gregory", "Hammond", "IN"],
    ["James", "Wynn", "Gary", "IN"],
    ["Craig", "Norman", "Schererville", "IN"]
  ]

console.log(convertTable(input));

Learn JavaScript Multidimensional Array By Examples, JavaScript doesn't provide the multidimensional array.This tutorial shows you how to create a JavaScript multidimensional array by using an array of arrays. The first parameter is the entire dataset, the second is an array of columns which define the nesting level. Expected JSON output: {key: "city", value: "Toronto", count: 1, children:

Your code contains three errors which can be fixed as follows:

  1. Start iterating your outer loop from i = 1 to skip headers, which are at index 0.

  2. Index into the headers array with tableData[0][j] instead of tableData[i][j][0], which is one dimension too many (accessing characters) and incorrectly includes the row index i.

  3. Index into your data table with tableData[i][j] instead of tableData[i][j][1], which again accesses characters within your strings.

Beyond this, I recommend paying attention to spacing, semicolons and camelCasing conventions. This will make your code easier to understand and debug.

Putting it together yields:

function convertTable(tableData) {
  var result = [];
  var key;
  var value;
  
  for (var i = 1; i < tableData.length; i++) { // start at 1 to skip headers
    var employee = {};
    
    for (var j = 0; j < tableData[i].length; j++) {
      key = tableData[0][j];   // remove character index and use [0][j]
      value = tableData[i][j]; // remove character index
      employee[key] = value;
    }
    
    result.push(employee);
  }
  
  return result;
}

var input = [
  ["first_name", "last_name", "city", "state"],
  ["June", "Gregory", "Hammond", "IN"],
  ["James", "Wynn", "Gary", "IN"],
  ["Craig", "Norman", "Schererville", "IN"]
];

console.log(convertTable(input));

Map and object: converting nested objects, However, it works on a classic structure like Array or an object. we encounter the same problem as before with nested objects: they are not converted into Map​  In this Dart/Flutter tutorial, we’re gonna look at ways to convert/parse JSON string into Object, Nested Object, how to parse JSON array, array of JSON objects into List. Finally, you can parse complex JSON into Nested Object (that also contains array as a field).

Array.prototype.flat(), The depth level specifying how deep a nested array structure should be flattened. Defaults to 1. Return value. A new array with the sub-array  This presents many problems though, mainly it introduces mutation into your code. Scary! The Solution. For me the ideal solution would be to convert the array into an object that looks like the

Indexed collections, This includes arrays and array-like constructs such as Array objects and The Array object has methods for manipulating arrays in various forEach(function(​element) { console.log(element) }) // logs each item in turn Arrays can be nested, meaning that an array can contain another array as an element. I recently had a problem whereby I wanted to filter objects in an array by comparing the values of nested properties of each object to see if there were duplicates. I have tried to explain as clearly as possible the scenario below and the solution I came up with.

Convert an object to associative array in PHP, How to convert Object's array to an array using JavaScript ? Multidimensional Associative Array in PHP · How to remove a key and its value from an associative​  I need to transform them into a new array with objects where the first key/value pair of each object is the date associated with an array of datetimes from the original array of objects. The second key/value pair is an array of the original objects which have a datetime date that matches the new date.

Comments
  • Please, take a look on this: stackoverflow.com/help/someone-answers. If the answer where helpful to you take care of follow those suggestions. Just remember that people spend time and effort helping you and will appreciate some feedback from you. There are some nice answers down there...
  • –1, not enough reduce ;)
  • :) is there ever such thing as "enough reduce"?