Javascript (ExtJS) iterate through object array, find duplicates and summarize into new object -


i've been working js (extjs) while , i'm trying following, assuming array of objects:

array tickets: [0]{ticket: 'ticket1', associated_val: 'ab'} [1]{ticket: 'ticket1', associated_val: 'xy'} [2]{ticket: 'ticket1', associated_val: 'cd'} [3]{ticket: 'ticket2', associated_val: 'yz'} [4]{ticket: 'ticket2', associated_val: 'ef'} [5]{ticket: 'ticket3', associated_val: 'ab'} [6]{ticket: 'ticket4', associated_val: null} 

i want obtain following:

summarized array: [0]{ticket: 'ticket1', associated_val_array: ['ab', 'xy', 'cd']} [1]{ticket: 'ticket2', associated_val_array: ['yz', 'ef']} [2]{ticket: 'ticket3', associated_val_array: ['ab']} [3]{ticket: 'ticket4', associated_val_array: []} 

i'm trying finding duplicates, comes mind similar this:

var summarizedarray = [];         (var = 0; < data.length -1; i++) {             if (data[i+1].ticket == data[i].ticket) {                 var myobj = {};                 var associated_val_array =[];                 associated_val_array.push(data[i].associated_val);                 myobj.set('ticket', data[i].ticket);                 myobj.set('associated_val_array', associated_val_array);                 summarizedarray.push(myobj);             }          } 

but i'm kind of blocked on how make works obtain summarized array.

any appreciated.

thanks in advance.

you can like

var array = [{    ticket: 'ticket1',    associated_val: 'ab'  }, {    ticket: 'ticket1',    associated_val: 'xy'  }, {    ticket: 'ticket1',    associated_val: 'cd'  }, {    ticket: 'ticket2',    associated_val: 'yz'  }, {    ticket: 'ticket2',    associated_val: 'ef'  }, {    ticket: 'ticket3',    associated_val: 'ab'  }, {    ticket: 'ticket4',    associated_val: null  }];    var newarray = [],    tmp = {},    item;  (var = 0; < array.length; i++) {    item = array[i];    if (!tmp[item.ticket]) {      tmp[item.ticket] = {        ticket: item.ticket,        associated_val_array: []      };      newarray.push(tmp[item.ticket]);    }    if (item.associated_val != null) {      tmp[item.ticket].associated_val_array.push(item.associated_val);    }  }    console.log(newarray)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -