group array of objects by id

后端 未结 8 531
抹茶落季
抹茶落季 2021-01-16 08:07

I am trying to loop through a array ob objects and group the items of the array into new arrays that have matching id:

API example:

    api_array [
          


        
相关标签:
8条回答
  • 2021-01-16 08:14

    var objs = [
       { id: 1, postcode: "xxx", street: "xxx", city: "xxx" },
       { id: 1, postcode: "xxx", street: "xxx", city: "xxx" },
       { id: 2, postcode: "xxx", street: "xxx", city: "xxx" },
       { id: 3, postcode: "xxx", street: "xxx", city: "xxx" }
    ];
    
    var result = objs.reduce(function(r, a) {
      r[a.id] = r[a.id] || [];
      r[a.id].push(a);
      return r;
    }, Object.create(null));
    
    console.log(result);

    0 讨论(0)
  • 2021-01-16 08:16

    group array of objects by id

    //Create a javascript array of objects containing key value pairs id, post 
    var api_array = [ 
           {id: 1, postcode: '10'}, 
           {id: 1, postcode: '11'}, 
           {id: 2, postcode: '20'}, 
           {id: 2, postcode: '21'}, 
           {id: 2, postcode: '22'}, 
           {id: 3, postcode: '30'} 
       ];  
    //result is a javascript array containing the groups grouped by id. 
    let result = []; 
    
    //javascript array has a method foreach that enumerates keyvalue pairs. 
    api_array.forEach(  
        r => { 
            //if an array index by the value of id is not found, instantiate it. 
            if( !result[r.id] ){  
                //result gets a new index of the value at id. 
                result[r.id] = []; 
            } 
            //push that whole object from api_array into that list 
            result[r.id].push(r); 
        }   
    ); 
    console.log(result[1]); 
    console.log(result[2]); 
    console.log(result[3]);
    

    Prints:

    [ { id: 1, postcode: '10' }, { id: 1, postcode: '11' } ]
    
    [ { id: 2, postcode: '20' }, 
      { id: 2, postcode: '21' },
      { id: 2, postcode: '22' } ]
    
    [ { id: 3, postcode: '30' } ]
    
    0 讨论(0)
  • 2021-01-16 08:18

    You could use the ids as properties of an object

    let api_array = [
        {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
        {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
    ];
    
    let grouped = groupArray(api_array);
    
    console.log(grouped);
    console.log(grouped[1]);
    
    function groupArray(myArray) {
        let grouped = {};
    
        for (let i = 0; i < myArray.length; i++) {
            let row = myArray[i];
            let group = grouped[row.id];
            if (!group) {
                group = [];
                grouped[row.id] = group;
            }
            group.push(row);
        }
    
        return grouped;
    }
    
    0 讨论(0)
  • 2021-01-16 08:21
    api_array [
           {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
    ];
    
    let result = [];
    for (let i = 0; i < numberOfGroupsIWantToMake; i++) {
        let newGroupArray = api_array.filter(obj => obj.id === i);
        result.push(newGroupArray);
    }
    
    return result;
    

    Note: this is A solution, but it's not as performant as we're going over the entire array n number of times.

    0 讨论(0)
  • 2021-01-16 08:23

    Try to acheive like this:

     
    
      var api_array = [
           {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
           {id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
       ];
       
      const result = api_array.reduce((acc, item) => {
        acc[`group_${item.id}`] = (acc[`group_${item.id}`] || []);
        acc[`group_${item.id}`].push(item);
        return acc;
      }, {});
    
    console.log(result);

    Note: The result will have keys group_1, group_2 ... instead group_one, group_two ...

    If you strictly need that, then make an array for key and values to convert 1 as one.

    0 讨论(0)
  • 2021-01-16 08:28

    https://jsfiddle.net/u4k16ojz/5/

    var result = new Array(4);
    api_array.forEach(function(item, index){
      if (!result[item.id]){
        result[item.id] = [];
      }
      result[item.id].push(item);
    })
    
    0 讨论(0)
提交回复
热议问题