How do I convert a nested array to a 'keyed' array in JavaScript?

Paul

I have a nested array that looks like this:

[["Organisation","ID","Name"],["ACME","123456","Bart Simpson"],["ACME","654321","Ned Flanders"],["ACME","1234","Edna Kabappel"],["Yahoogle","666666","Bon Scott"],["Yahoogle","99999","Neil Young"],["Yahoogle","111111","Shania Twain"]]

The first value in each array is the name of an organisation that an ID and name can belong to.

I am trying to find the simplest way to group all instances where an ID and name belong to the same company, under one 'key'.

So the above would result in something like this:

{
   "ACME": [
      {
         "ID": 123456,
         "Name": "Bart Simpson"
      },
      {
         "ID": 654321,
         "Name": "Ned Flanders"
      },
      {
         "ID": 1234,
         "Name": "Edna Kabappel"
      }
   ],
   "Yahoogle": [
      {
         "ID": 666666,
         "Name": "Bon Scott"
      },
      {
         "ID": 99999,
         "Name": "Neil Young"
      },
      {
         "ID": 111111,
         "Name": "Shania Twain"
      }
   ]
}

I have been playing around with for loops but I'm ending up with many many lines of code, trying to detect when the company name is different from the previous, and getting into a real mess with things.

I have searched a lot here trying to find something similar but have not had any luck.

I have only just started coding again for person interest after about 18 years and I'm very novice again.

Thank you in advance for any assistance.

Frenchy

lot of solutions to arrive at same result, one using lambda and reduce: this is a generic solution, just adapt the output push to build your final json.

const datas = [
  ["Organisation", "ID", "Name"],
  ["ACME", "123456", "Bart Simpson"],
  ["ACME", "654321", "Ned Flanders"],
  ["ACME", "1234", "Edna Kabappel"],
  ["Yahoogle", "666666", "Bon Scott"],
  ["Yahoogle", "99999", "Neil Young"],
  ["Yahoogle", "111111", "Shania Twain"]
];

const titles = datas.shift()
const groupBy = (x,f)=>x.reduce((a,b)=>((a[f(b)]||=[])
                        .push({[titles[1]]:b[1], [titles[2]]:b[2]}),a),{});

const result = groupBy(datas, v => v[0])

console.log(result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an array of strings to keyed object in javascript/lodash

How do you convert an array to a nested object in javascript?

How to convert nested set into nested array in javascript?

How do I convert this php array into a JavaScript array of objects?

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

How do I convert this to an array?

How do I add data I have keyed in a EditText box into an array to list in another activity?

How to Convert nested json to array of object in Javascript?

How to convert array into nested objects in javascript

How to convert nested object to array of object in javascript?

how convert my nested object to array in javascript

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

How do I convert an excel table to a javascript array?

How do I convert a C# List<string[]> to a Javascript array?

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

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

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

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

How do i convert this string into an array type in - Javascript

javascript array with some elements that are keyed

Javascript Convert an array to map keyed with the array property and store the corresponding duplicate key values as array

How to convert nested array of objects into one array of objects using javaScript?

How do I convert a javascript object array to a string array of the object attribute I want?

how to do sort in nested array javascript

How do I sort an array of nested types?

How do I update an Array nested in a Hash?

How do I push to an array that is nested in an object?

How do I update values in a nested array?

How do I group data that is nested in an array?