如果特定值相同,我想要一个合并子数组的函数

我有如下二维数组:

[ ['Procare Fitted 医院床底床单,象牙色','DT4098','Demo',null,12300,2],['Essential Medical Supply Deluxe Complete Hospital Bed Set','ERT665','Demo',null, 11790, 2],['博士。Morepen St 04 声学听诊器', 'DT4098', 'Demo', null, 21237, 2], ['CosmoCare KLife 5 Para Patient Monitor', 'YTT690', 'Demo', null, 11000, 2], ['CosmoCare KLife 5 Para Patient Monitor', 'YTT690', 'Demo', null, 11000, 2], ['CosmoCare KLife 5 Para Patient Monitor', 'YTT690', 'Demo', null, 11000, 2], ['CosmoCare KLife 5 Para Patient Monitor', 'YTT690', 'Demo', null, 11000, 2], ['Dr. Morepen St 04 声学听诊器', 'DT4098', 'Demo', null, 21237, 2],

我只想根据子数组的第一个索引从多维数组中删除重复值。如果第一个索引值相同,则删除重复的子数组。

[ ['Procare Fitted 医院床底床单,象牙色','DT4098','Demo',null,12300,6],['Essential Medical Supply Deluxe Complete Hospital Bed Set','ERT665','Demo',null, 11790, 6], ['CosmoCare KLife 5 Para Patient Monitor', 'YTT690', 'Demo', null, 11000, 4]];


慕姐4208626
浏览 116回答 1
1回答

萧十郎

我希望这对你有帮助。var myArray = [ [ 'Procare Fitted Hospital Bed Bottom Sheet, Ivory',    'DT4098',    'Demo',    null,    12300,    2],  [ 'Essential Medical Supply Deluxe Complete Hospital Bed Set',    'ERT665',    'Demo',    null,    11790,    2],  [ 'Dr. Morepen St 04 Acoustic Stethoscope',    'DT4098',    'Demo',    null,    21237,    2],  [ 'CosmoCare KLife 5 Para Patient Monitor',    'YTT690',    'Demo',    null,    11000,    2],  [ 'CosmoCare KLife 5 Para Patient Monitor',    'YTT690',    'Demo',    null,    11000,    2],  [ 'CosmoCare KLife 5 Para Patient Monitor',    'YTT690',    'Demo',    null,    11000,    2],  [ 'CosmoCare KLife 5 Para Patient Monitor',    'YTT690',    'Demo',    null,    11000,    2],  [ 'Dr. Morepen St 04 Acoustic Stethoscope',    'DT4098',    'Demo',    null,    21237,    2],  [ 'Essential Medical Supply Deluxe Complete Hospital Bed Set',    'ERT665',    'Demo',    null,    11790,    2],  [ 'Essential Medical Supply Deluxe Complete Hospital Bed Set',    'ERT665',    'Demo',    null,    11790,    2] ];   let duplicates = [];    let output = myArray.reduce((t, i) => {      if(duplicates.includes(i[1])) {        let myArray = t.filter((item) => item[1] == i[1])[0];        myArray[5] += i[5];        t = t.filter((item) => item[1] != i[1]);        i = myArray;      } else {        duplicates.push(i[1]);      }      t.push(i);            return t;    }, []);        console.log(output); 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript