【问题标题】:How can be convert object arrays into multidimensional arrays in node.js?node.js中如何将对象数组转换为多维数组?
【发布时间】:2015-09-03 18:00:22
【问题描述】:

我们在 node.js 中有一个以下格式的对象数组,我们希望这个数组在多维数组中。

对象数组:

[ { 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 } ]

我们希望这个对象数组采用以下格式(多维数组)。请建议我们如何将其转换为以下格式

[ [ { 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 } ],
  ]  

【问题讨论】:

  • 我们需要在页面列表中使用这个多维数组。
  • 然后呢?你似乎没有付出任何努力来解决这个问题。你的尝试在哪里? SO 不是免费的开发服务。
  • 嘿,安迪,谢谢你的建议..

标签: javascript arrays node.js


【解决方案1】:

希望这有助于 createMultiArray 根据您的对象 ID 创建所需的数组:

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);

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2016-11-28
    • 2012-11-14
    • 2017-05-05
    • 2014-05-25
    • 2021-01-06
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多