0

I am using the standard groupBy function, modified by returning [hash] instead of hash. This returns an array of objects.

How can I instead output an array of arrays?

Standard groupBy Function

function groupBy(array, property) {
  var hash = {};
  for (var i = 0; i < array.length; i++) {
    if (!hash[array[i][property]]) hash[array[i][property]] = [];
    hash[array[i][property]].push(array[i]);
  }
  return [hash];
}

Output

[{key1:[array]}, {key2:[array]}, {key3:[array]}]

Desired Output

[[array1], [array2], [array3]]

Ultimately, I would like to run a for each over the output of arrays to pass into another function.

For instance,

let group = groupItemBy(cart, 'id');
console.log(group.length); // should be greater than 1 for multiple indexes
6
  • 1
    Your desired output is not valid. Please check. Commented Feb 18, 2018 at 15:42
  • What do you mean? Can you be more specific? Commented Feb 18, 2018 at 15:44
  • 1
    [key2: is invalid JavaScript syntax. Either you want a plain object with named keys or an array. You cannot have some half-half. Commented Feb 18, 2018 at 15:44
  • 1
    Your key and array are separated by : which is not valid. Commented Feb 18, 2018 at 15:44
  • OK. Thank you. I updated the question. Commented Feb 18, 2018 at 15:46

2 Answers 2

3

Instead of

return [hash];

Do:

return Object.values(hash);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure how to get this to work with nested data. See: jsbin.com/cemedebugu/edit?js,console
That really is a different question, as there the property does not exist in the objects, but in nested objects. This will also mean you need to better define the function arguments so that it is clear where this property is to be located. If you cannot make it work as you expect, I would suggest posting that as a new question.
0

Assuming you return hash and not [hash] , just get the objects values:

 Object.values(groupItemBy(cart, 'carrierId'))

Alternatively build up the hashtable and the result in parallel:

 function group(array, key){
   const hash = {}, result = [];

   for(const obj of array){
     if(hash[ obj[key] ]){
       hash[ obj[key] ].push(obj);
     } else {
       result.push( hash[ obj[key] ] = [obj]);
    }
  }

  return result;
}

9 Comments

I think OP function is returning an array.
I'm not seeing the difference between logging Object.values(groupItemBy(cart, 'carrierId')) and group...
Not sure if it's causing problems, but I am actually sorting by a value, so when I return hash and log Object... everything gets put into a single array.
@kvna what do you mean with sorting by a value ?
Would you please take a look at this fiddle? jsfiddle.net/04v1uczr
|

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.