-1

I have a feeling this question already exists, just can't find it. Is there a way to take a 2d array with x rows and 2 columns and merge multiple values into one row based on it having the same element in the first column?

[['Needs Work', 'Joe'], ['Needs Work', 'Jill'], ['Needs Work', 'Jack'], ['Complete', 'Sean'], ['Complete', 'Joe'], ['Not Started', 'Laura'], ['Needs Work', 'Jack']]

So that it looks like this

[ [ 'Needs Work', 'Joe,Jill,Jack,Jack' ],
  [ 'Complete', 'Sean,Joe' ],
  [ 'Not Started', 'Laura' ] ]
SLee
  • 13
  • 4
  • Welcome to SO. It's difficult to tell what the problem is. Everyone would find code examples easier to read so you should update your question. In addition you should add the code you've attempted to solve the problem as a [mcve]. You might find reading the site [help section](https://stackoverflow.com/help) useful when it comes to [asking a good question](https://stackoverflow.com/help/how-to-ask), and this [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – Andy Dec 03 '22 at 20:22
  • 1
    As I mentioned you need to add in the array to the question so we can see it's structure. Using a table as a replacement limits us to how much we can help. – Andy Dec 03 '22 at 20:48
  • Does this answer your question? [Javascript - Filter and/or Group Array of Objects by property](https://stackoverflow.com/questions/67628217/javascript-filter-and-or-group-array-of-objects-by-property) – IT goldman Dec 03 '22 at 21:18
  • I was able to figure it out. – SLee Dec 03 '22 at 21:19

2 Answers2

0

Not sure if this the most efficient way.

function myFunction() {
  var array = [['Needs Work', 'Joe'], ['Needs Work', 'Jill'], ['Needs Work', 'Jack'], ['Complete', 'Sean'], ['Complete', 'Joe'], ['Not Started', 'Laura'], ['Needs Work', 'Jack']];
  const extractColumn = (arr, n) => arr.map(row => row[n])
  var list = [...new Set(extractColumn(array, 0))]//gets set of distict values from first column
  // console.log(list)
  var condensedArray = []
  for (var i = 0; i < list.length; i++) {
    var filteredArray = array.filter(row => row[0] == list[i])//filters array to only to current value
    // console.log(filteredArray)
    condensedArray.push([list[i], extractColumn(filteredArray, 1).toString()])

  }
  console.log(condensedArray)
}
SLee
  • 13
  • 4
-1

Considering the answer that you posted, I wrote the following function:

function create_union_array(initial_array) {
  const temporal_object = {};
  for (i in initial_array) {
    const work_state = initial_array[i][0];
    const people_name = initial_array[i][1];
    if (temporal_object[work_state] == undefined) {
      temporal_object[work_state] = [people_name]
    } else {
      temporal_object[work_state].push(people_name)
    }
  }
  const output_array = [];
  let iteration = 0;
  for (i in temporal_object) {
    output_array[iteration] = [i, temporal_object[i]]
    iteration++;
  }
  return output_array;
}

Unlike yours, instead of returning the names in a concatenated string, this one returns the names in an array, in case you need to work with the names afterwards this would be a better option.

LucaDCS
  • 49
  • 5