简体   繁体   English

通过基于javascript中每个对象的属性值对对象数组进行分组来创建新的对象数组

[英]Create new array of objects by grouping array of objects based on property value of each object in javascript

I have an array of objects like below, I want to create new array of objects by grouping array of java script objects based on property value of each object in java script 我有一个如下的对象数组,我想通过基于Java脚本中每个对象的属性值对Java脚本对象数组进行分组来创建新的对象数组

Here I want to group objects based on Group ID and store it into an array of object with unique group ID. 在这里,我想根据组ID对对象进行分组,并将其存储到具有唯一组ID的对象数组中。

This is my object: 这是我的对象:

var Object = [
 {"Name":15,"GroupID":1,"ComplexObject":1}`object1`    
 {"Name":16,"GroupID":1,"ComplexObject":1}`object2`   
 {"Name":17,"GroupID":2,"ComplexObject":2}`object3
 {"Name":18,"GroupID":2,"ComplexObject":2}`object4`
 {"Name":15,"GroupID":3,"ComplexObject":3}`object5`
 {"Name":16,"GroupID":3,"ComplexObject":3}`object6`
 {"Name":17,"GroupID":4,"ComplexObject":4}`object7`
 {"Name":18,"GroupID":4,"ComplexObject":4}`object8` ];
var new_arr = [];

Objects.forEach(function(object) {
  if (new_arr.length <= object.GroupID) {
    new_arr[object.GroupID] = [];
  }
  new_arr[object.GroupID].push(object);
});

Update: if you want the length of the array, just use: 更新:如果您想要数组的长度,只需使用:

var new_arr = [];

Objects.forEach(function(object) {
  if (new_arr.length < object.GroupID) {
    new_arr[object.GroupID - 1] = [];
  }
  new_arr[object.GroupID - 1].push(object);
});

and you will get this: 你会得到这个:

结果

You can try this way by checking the group id and pushing it to the array object. 您可以通过检查组ID并将其推入数组对象来尝试这种方式。

  var data = [ 
                {"Name":15,"GroupID":1,"ComplexObject":1},
                {"Name":16,"GroupID":1,"ComplexObject":1},
                {"Name":17,"GroupID":2,"ComplexObject":2},
                {"Name":18,"GroupID":2,"ComplexObject":2},
                {"Name":15,"GroupID":3,"ComplexObject":3},
                {"Name":16,"GroupID":3,"ComplexObject":3},
                {"Name":17,"GroupID":4,"ComplexObject":4},
                {"Name":18,"GroupID":4,"ComplexObject":4}
             ];

  console.log("Original Data : ", data);

  var previousGroupId;
  var newObject = new Object();
  for(index in data){
      var groupId = data[index].GroupID;
      if(groupId != previousGroupId){
          var newGroup = "GroupId" + groupId;
          newObject[newGroup] = new Array();
          for(index in data){
              if(data[index].GroupID == groupId){
                  var customObject = {
                      "GroupID" : groupId,
                      "Name" : data[index].Name,
                      "ComplexObject" : data[index].ComplexObject
                  };
                  newObject[newGroup].push(customObject);        
              }
          }
      }
      previousGroupId = groupId;
  }

  console.log("Grouped data : ", newObject);

jsFiddle jsFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对对象数组进行分组并为每个对象分配新的组合值 - Grouping an array of objects and assign new combined value to each object 基于键和值分组的 JavaScript 对象数组 - JavaScript array of objects grouping based on key and value 在javascript中的对象数组中从具有空值的数组创建一个新属性 - Create a new property from an array with null value in an array of objects in javascript 根据长度创建对象数组; 每个 object 都有一个以索引 + 1 作为其值的属性 - Create array of objects based on length; each object has a property with the index + 1 as its value 对象数组:为每个具有值的指定键创建新对象 - Array of Objects: Create new object for each specified key that has a value 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 根据 Javascript 中的属性值从数组的 object 中获取唯一对象 - Getting unique objects from object of array based on property value in Javascript 根据现有对象的属性值创建新的对象数组 object 将现有对象添加到新数组 - Create a new array of objects by property value from existing object add existing objects to new array 根据对象的 id 创建一个新的对象数组 - Create a new array of object based on the ids of the objects 如何基于对象数组创建新对象 - How to create a new Object based on an array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM