简体   繁体   中英

How can be convert object arrays into multidimensional arrays in node.js?

We have a object arrays the below format in node.js and we want to this array in multidimensional arrays.

object arrays :

[ { id: 4,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 1,
    attribute_id: 6,
    attribute_label: 'level4',
    attribute_order: 1,
    attribute_name: 'level4_1'
  { id: 4,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 1,
    attribute_id: 7,
    attribute_label: 'level5',
    attribute_order: 2,
    attribute_name: 'level5_1'
  { id: 15,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 2,
    attribute_id: null,
    attribute_label: null,
    attribute_order: null,
    attribute_name: null },
  { id: 5,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 3,
    attribute_id: 8,
    attribute_label: 'level6',
    attribute_order: 1,
    attribute_name: 'level6_1'
  { id: 5,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 3,
    attribute_id: 9,
    attribute_label: 'level7',
    attribute_order: 2,
    attribute_name: 'level7_1'
  { id: 6,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 4,
    attribute_id: 10,
    attribute_label: 'level8',
    attribute_order: 1,
    attribute_name: 'level8_1'
  { id: 14,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 5,
    attribute_id: null,
    attribute_label: null,
    attribute_order: null,
    attribute_name: null } ]

and we want to this object array in below format (multidimensional arrays). please suggest that how can we convert it the below formate

[ [ { id: 4,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 1,
      attribute_id: 6,
      attribute_label: 'level4',
      attribute_order: 1,
      attribute_name: 'level4_1' },
    { id: 4,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 1,
      attribute_id: 7,
      attribute_label: 'level5',
      attribute_order: 2,
      attribute_name: 'level5_1' } ],

   [ { id: 15,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 2,
      attribute_id: null,
      attribute_label: null,
      attribute_order: null,
      attribute_name: null } ],

  [ { id: 5,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 3,
      attribute_id: 8,
      attribute_label: 'level6',
      attribute_order: 1,
      attribute_name: 'level6_1' },
    { id: 5,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 3,
      attribute_id: 9,
      attribute_label: 'level7',
      attribute_order: 2,
      attribute_name: 'level7_1' } ],
  [ { id: 6,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 4,
      attribute_id: 10,
      attribute_label: 'level8',
      attribute_order: 1,
      attribute_name: 'level8_1' } ],
  [ { id: 14,
      process_id: 94,
      process_type: 'Section',
      process_type_order: 5,
      attribute_id: null,
      attribute_label: null,
      attribute_order: null,
      attribute_name: null } ],
  ]  

Hope this helps createMultiArray creates your desired Array based on your objects id's:

var array = [ { id: 4,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 1,
    attribute_id: 6,
    attribute_label: 'level4',
    attribute_order: 1,
    attribute_name: 'level4_1'
              },
  { id: 4,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 1,
    attribute_id: 7,
    attribute_label: 'level5',
    attribute_order: 2,
    attribute_name: 'level5_1'
              },
  { id: 15,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 2,
    attribute_id: null,
    attribute_label: null,
    attribute_order: null,
    attribute_name: null },
  { id: 5,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 3,
    attribute_id: 8,
    attribute_label: 'level6',
    attribute_order: 1,
    attribute_name: 'level6_1',
  },
  { id: 5,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 3,
    attribute_id: 9,
    attribute_label: 'level7',
    attribute_order: 2,
    attribute_name: 'level7_1'
  },
  { id: 6,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 4,
    attribute_id: 10,
    attribute_label: 'level8',
    attribute_order: 1,
    attribute_name: 'level8_1'
  },
  { id: 14,
    process_id: 94,
    process_type: 'Section',
    process_type_order: 5,
    attribute_id: null,
    attribute_label: null,
    attribute_order: null,
    attribute_name: null } ]


function createMultiArray(array){
  var newArray = [];
  array.forEach(function(obj,index){
    var subarray;
    var subArrayExists = false;
    for(var sub=0; sub < newArray.length; sub++){
      subarray = newArray[sub];
      if(subarray.some(function(subobj){ 
        return subobj.id == obj.id;
      })){
         subArrayExists = true;
         break;
      }
    }

    if(!subArrayExists){
      newArray.push([obj]);
    }
    else{
      subarray.push(obj);
    }

  });
  return newArray;
}


createMultiArray(array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM