0

My goal is to populate an array, by assigning empty arrays to its current values, and then removing original values, to finally get an array of arrays.

[ { id_menu_item: 3, 
  title: 'Preferences', 
  url: '/user/settings', 
  icon: 'settings', 
  is_parent: false, 
  id_parent: 1 
}, 
{ 
  id_menu_item: 4, 
  title: 'Log out', 
  url: '/logout', 
  icon: 'power_settings_new', 
  is_parent: false, 
  id_parent: 1 
}, 
{ 
  id_menu_item: 6, 
  title: 'Images', 
  url: null, 
  icon: 'collections', 
  is_parent: true, 
  id_parent: 5 
} ]

I'm getting this array, and I need to sort it by id_parent. I have an array with the different id_parent (on this case, I have var ids = [1, 5] )

I want to achieve the following:

[
1 = [
  {..., id_parent = 1}, 
  {..., id_parent = 1}], 
5 = [
  {..., id_parent = 5}
]]

where the ... are not spread operators, just omitting the rest of the object for clarity sake. Do you guys see what I mean more precisely now ?

DrunkDevKek
  • 445
  • 4
  • 15
  • 1
    Are you just trying to loop through all values and set it to `[]`? I don't think there's a way to set all elements in the array to the same value without looping, [Create an array with same element repeated multiple times in JavaScript](https://stackoverflow.com/questions/12503146/create-an-array-with-same-element-repeated-multiple-times-in-javascript) may be useful – Patrick Barr May 22 '17 at 13:51
  • 3
    Are these intermediary steps essential or do you just care that you get an array of arrays? I'm not clear on why an object wouldn't be more useful for your purposes. Could you explain why you are doing things this way? – Douglas Tyler Gordon May 22 '17 at 13:52
  • 1
    please add soem use cases. – Nina Scholz May 22 '17 at 13:52
  • Basically, I'm using facebook's `dataloader`, which requires to return an array. I have to format it this way, else I would indeed be using an object! – DrunkDevKek May 22 '17 at 13:54
  • Wait, I'll make it clearer for everyone – DrunkDevKek May 22 '17 at 13:57
  • @pds42 https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Stephen S May 22 '17 at 13:57
  • @pds42 edit these into the question – Stephen S May 22 '17 at 14:06
  • @pds42, this can be done with my answer. However, your wanted output does not make sense to me... – Jonas Wilms May 22 '17 at 14:07
  • @pds42 so all your really want to do is sort an array of objects, by one object property? – Stephen S May 22 '17 at 14:08
  • Edited. @StephenS Nah I know how to do this, but where I'm struggling is how do I keep the keys without overriding them with the value? Currently, when I try solutions I get ```[ [], [], []]``` instead of ```[1 = [], 2 = [], 3 = []]``` – DrunkDevKek May 22 '17 at 14:12
  • @pds42 you can't. an array is not an object, or a php array. you could have a second array with the in indexes, maybe – Stephen S May 22 '17 at 14:13
  • Okay, thank you. I was starting to wonder if I was not seeing something obvious. I'll do something else then, unless someone else can help on this :) – DrunkDevKek May 22 '17 at 14:18
  • @stephens that does not make much sense. Arrays have keys... – Jonas Wilms May 22 '17 at 14:45
  • @Jonasw they have keys, but they are `0, 1, 2, ...`. You can't create associative arrays the way you can in PHP - you can't do `key`-`name` pairs in JS arrays (you can in JS objects, but JS objects aren't ordered like an array). – Stephen S May 22 '17 at 16:53
  • @Stephen S : the keys are integers. That means we could create a sparse array. – Jonas Wilms May 22 '17 at 17:10

2 Answers2

2
var array1=[1,2,3].map(el=>[]);

Simply map your array to arrays. If these arrays should contain the former objects, you might do:

var array1=[1,2,3].map(el=>[el]);

So in your case, you may do:

var data=[ { id_menu_item: 3, title: 'Preferences', url: '/user/settings', icon: 'settings', is_parent: false, id_parent: 1 }, { id_menu_item: 4, title: 'Log out', url: '/logout', icon: 'power_settings_new', is_parent: false, id_parent: 1 }, { id_menu_item: 6, title: 'Images', url: null, icon: 'collections', is_parent: true, id_parent: 5 } ];

var ids=[1,5];

var sorted=ids.map(id=>data.filter(el=>el.id_parent===id));

This will map the ids to the objects with that certain ids...

And by the way, arrays have keys too:

[1,2,3]

equals:

{
0:1,
1:2,
2:3,
prototype:Array.prototype
}
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
1

It looks like, that you need grouping with id_parent property.

This propsal uses a hash table as closure and checks if id_parent is in the hash table. If not, a new array is assigned to the has table and pushed to the result set. Then the actual object is pushed to the reference of the hash table.

var data =[ { id_menu_item: 3, title: 'Preferences', url: '/user/settings', icon: 'settings', is_parent: false, id_parent: 1 }, { id_menu_item: 4, title: 'Log out', url: '/logout', icon: 'power_settings_new', is_parent: false, id_parent: 1 }, { id_menu_item: 6, title: 'Images', url: null, icon: 'collections', is_parent: true, id_parent: 5 } ],
    result = data.reduce(function (hash) {
        return function (r, a) {
            if (!hash[a.id_parent]) {
                r.push(hash[a.id_parent] = []);
            }
            hash[a.id_parent].push(a);
            return r;
        };
    }(Object.create(null)), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324