1

I am trying to create an array of object that will have multiple children. I have a table with data like this:

| PARENT   | CHILD  |
---------------------
| 123456   | 123    |
| 123456   | 124    |
| 123456   | 125    |
| 123457   | 345    |
| 123457   | 346    |
| 123457   | 347    |
....

I wish to get an array similar to this:

var arr_nos = { 123456 : [123, 124, 125], 123457 : [345, 346, 347]}

During a loop, I have this:

var arr_nos = [];
$('#table tbody tr').each(function( index ) {
   var parent_no = elem.find('.parent_no').html();
   var child_id = elem.find('.child_id ').html();
   if(parent_no != '') {
      child = [
         child_id
      ]
      arr_nos.push(parent_no, child);
   }

});

But this results in:

["123456", Array(1), "123456", Array(1),"123456", Array(1), "123457", Array(1), "123457", Array(1), "123457", Array(1)]
Judson Cooper
  • 51
  • 1
  • 7
  • So your example that you want to get `var arr_nos = { 123456 : [123, 124, 125], 123457 : [345, 346, 347]}` is an object of arrays – Sam Dec 24 '20 at 15:57
  • You said you want to get object but you create array and push values to it – Omri Attiya Dec 24 '20 at 15:58
  • Your title says you want a multidimensional array, but your sample suggests it is just an object with value as an array. So what is it? – Nikhil Patil Dec 24 '20 at 16:05
  • Yes, my title is incorrect. The example is what I am trying to acheive. – Judson Cooper Dec 24 '20 at 16:14
  • Does this answer your question? [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – Wyck Dec 24 '20 at 16:21

3 Answers3

1

You can use this loop :

var arr_nos = {};
$('#table tbody tr').each(function( index,elem ) {
   var parent_no = $(elem).find('.parent_no').html();
   var child_id = $(elem).find('.child_id ').html();
   if(parent_no != '') {
      if(!arr_nos[parent_no]) arr_nos[parent_no] = [];
      arr_nos[parent_no].push(child_id);
   }

});

console.log(arr_nos);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
 <tbody>
  <tr>
    <th class='parent_no'>1</th>
    <th class='child_id'>11</th>
  </tr>
  <tr>
    <th class='parent_no'>2</th>
    <th class='child_id'>22</th>
  </tr>
  <tr>
    <th class='parent_no'>3</th>
    <th class='child_id'>33</th>
  </tr>
  <tr>
    <th class='parent_no'>2</th>
    <th class='child_id'>444</th>
  </tr>
  </tbody>
</table>
Arash Hasanzade
  • 490
  • 3
  • 12
0

To get the object of arrays in your example you would have to do

  arr_nos[parent_no] = child

and would have to declare the object as

arr_nos = {}

instead of

arr_nos = []

But this is an object of arrays, not an array of objects. Either your example is wrong, or your definition is wrong

I'm not sure how to obtain your specific example with {123456: [123, 124, 125], 123457: [345, 346, 347]}. You'll have to specify what criteria you have to make decide what the keys of this object are.

Sam
  • 1,765
  • 11
  • 82
  • 176
0

Instead of:

var arr_nos = []
arr_nos.push(parent_no, child)

do:

let arr_nos = {}
arr_nos[parent_no] = [...arr_nos[parent_no], child]
FahDev
  • 357
  • 1
  • 7