Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get all objects between two column from a javascript array

I have this sample array.

     [
        {
            __EMPTY: 'POS:',
            __EMPTY_1: 'LOUN - LOUNGE',
        },
        {
            __EMPTY: 44946,
            __EMPTY_1: 'LB015260',
            __EMPTY_2: '15:04',
            __EMPTY_3: 'L-15',
            __EMPTY_4: 1402,

            __EMPTY_5: 'AKASH',
            __EMPTY_6: 66.78,
            __EMPTY_7: 0,
        },
        {
            __EMPTY: 44946,
            __EMPTY_1: 'LB015260',
            __EMPTY_2: '15:04',
            __EMPTY_3: 'L-15',
            __EMPTY_4: 1402,

            __EMPTY_5: 'AKASH',
            __EMPTY_6: 66.78,
            __EMPTY_7: 0,
        },
        {
            __EMPTY: 44946,
            __EMPTY_1: 'LB015260',
            __EMPTY_2: '15:04',
            __EMPTY_3: 'L-15',
            __EMPTY_4: 1402,

            __EMPTY_5: 'AKASH',
            __EMPTY_6: 66.78,
            __EMPTY_7: 0,
        },
        {
            __EMPTY: 'POS:',
            __EMPTY_1: 'PARTY - PARTY',
        },
        {
            __EMPTY: 44946,
            __EMPTY_1: 'LB015260',
            __EMPTY_2: '15:04',
            __EMPTY_3: 'L-15',
            __EMPTY_4: 1402,

            __EMPTY_5: 'AKASH',
            __EMPTY_6: 66.78,
            __EMPTY_7: 0,
        },
        {
            __EMPTY: 44946,
            __EMPTY_1: 'LB015260',
            __EMPTY_2: '15:04',
            __EMPTY_3: 'L-15',
            __EMPTY_4: 1402,

            __EMPTY_5: 'AKASH',
            __EMPTY_6: 66.78,
            __EMPTY_7: 0,
        },
        {
            __EMPTY: 'POS:',
            __EMPTY_1: 'TERRACE - TERRACE',
        },
        {
            __EMPTY: 44946,
            __EMPTY_1: 'LB015260',
            __EMPTY_2: '15:04',
            __EMPTY_3: 'L-15',
            __EMPTY_4: 1402,

            __EMPTY_5: 'AKASH',
            __EMPTY_6: 66.78,
            __EMPTY_7: 0,
        },
    ];

How can I get array like below

[{
  'LOUN - LOUNGE':
    [{
      __EMPTY: 44946,
      __EMPTY_1: 'LB015260',
      __EMPTY_2: '15:04',
      __EMPTY_3: 'L-15',
      __EMPTY_4: 1402,

      __EMPTY_5: 'AKASH',
      __EMPTY_6: 66.78,
      __EMPTY_7: 0,
    },
    {
      __EMPTY: 44946,
      __EMPTY_1: 'LB015260',
      __EMPTY_2: '15:04',
      __EMPTY_3: 'L-15',
      __EMPTY_4: 1402,

      __EMPTY_5: 'AKASH',
      __EMPTY_6: 66.78,
      __EMPTY_7: 0,
    },
    {
      __EMPTY: 44946,
      __EMPTY_1: 'LB015260',
      __EMPTY_2: '15:04',
      __EMPTY_3: 'L-15',
      __EMPTY_4: 1402,

      __EMPTY_5: 'AKASH',
      __EMPTY_6: 66.78,
      __EMPTY_7: 0,
    }],
        
  'PARTY - PARTY':
        
    [{
      __EMPTY: 44946,
      __EMPTY_1: 'LB015260',
      __EMPTY_2: '15:04',
      __EMPTY_3: 'L-15',
      __EMPTY_4: 1402,

      __EMPTY_5: 'AKASH',
      __EMPTY_6: 66.78,
      __EMPTY_7: 0,
    },
    {
      __EMPTY: 44946,
      __EMPTY_1: 'LB015260',
      __EMPTY_2: '15:04',
      __EMPTY_3: 'L-15',
      __EMPTY_4: 1402,

      __EMPTY_5: 'AKASH',
      __EMPTY_6: 66.78,
      __EMPTY_7: 0,
    }],
  'TERRACE - TERRACE':
    [{
      __EMPTY: 44946,
      __EMPTY_1: 'LB015260',
      __EMPTY_2: '15:04',
      __EMPTY_3: 'L-15',
      __EMPTY_4: 1402,
      __EMPTY_5: 'AKASH',
      __EMPTY_6: 66.78,
      __EMPTY_7: 0,
    }]
}]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use reduce to keep track of the current key and collect the objects in the corresponding subarray:

const data = [{__EMPTY: 'POS:',__EMPTY_1: 'LOUN - LOUNGE',},{__EMPTY: 44946,__EMPTY_1: 'LB015260',__EMPTY_2: '15:04',__EMPTY_3: 'L-15',__EMPTY_4: 1402,__EMPTY_5: 'AKASH',__EMPTY_6: 66.78,__EMPTY_7: 0,},{__EMPTY: 44946,__EMPTY_1: 'LB015260',__EMPTY_2: '15:04',__EMPTY_3: 'L-15',__EMPTY_4: 1402,__EMPTY_5: 'AKASH',__EMPTY_6: 66.78,__EMPTY_7: 0,},{__EMPTY: 44946,__EMPTY_1: 'LB015260',__EMPTY_2: '15:04',__EMPTY_3: 'L-15',__EMPTY_4: 1402,__EMPTY_5: 'AKASH',__EMPTY_6: 66.78,__EMPTY_7: 0,},{__EMPTY: 'POS:',__EMPTY_1: 'PARTY - PARTY',},{__EMPTY: 44946,__EMPTY_1: 'LB015260',__EMPTY_2: '15:04',__EMPTY_3: 'L-15',__EMPTY_4: 1402,__EMPTY_5: 'AKASH',__EMPTY_6: 66.78,__EMPTY_7: 0,},{__EMPTY: 44946,__EMPTY_1: 'LB015260',__EMPTY_2: '15:04',__EMPTY_3: 'L-15',__EMPTY_4: 1402,__EMPTY_5: 'AKASH',__EMPTY_6: 66.78,__EMPTY_7: 0,},{__EMPTY: 'POS:',__EMPTY_1: 'TERRACE - TERRACE',},{__EMPTY: 44946,__EMPTY_1: 'LB015260',__EMPTY_2: '15:04',__EMPTY_3: 'L-15',__EMPTY_4: 1402,__EMPTY_5: 'AKASH',__EMPTY_6: 66.78,__EMPTY_7: 0,},];
    
const [result] = data.reduce(([acc, key], obj) =>
    obj.__EMPTY === "POS:" 
             ? [acc, obj.__EMPTY_1]
             : [((acc[key] ??= []).push(obj), acc), key]
, [{}]);

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading