function createoffer_items(q ={'custid':'A-0010', 'regional':'EU'}){
const history = [['A-0010', 'MOD', 'GG-1169268', 10, 'CT-999001-1'] ,['A-0010', 'MOD', 'GG-999002', 20, 'CT-999001-2'] ,
['A-0010', 'MOD', 'GG-999003', 30, 'CT-999001-3'] ,['A-0010', 'MOD', 'GG-999002', 40, 'CT-999001-4'] ,
['A-0022', 'MOD', 'GG-999066', 50, 'CT-999001-5'] ,['A-0022', 'MOD', 'GG-0019149', 60, 'CT-999001-6'] ,
['A-0022', 'MOD', 'GG-0027943', 70, 'CT-999001-7'] ,['A-0370', 'EU', 'GG-0069268', 10, 'CT-999001-8'] ,
['A-0370', 'EU', 'GG-0069269', 20, 'CT-999001-9'] ,['A-0370', 'EU', 'GG-0049451', 30, 'CT-999001-10'] ,
['A-0372', 'AO', 'GG-0049454', 40, 'CT-999001-11'] ,['A-0372', 'AO', 'GG-0049455', 50, 'CT-999001-12'] ,
['A-0372', 'AO', 'GG-0065355', 60, 'CT-999001-13'] ,['A-0382', 'LAT', 'GG-0049455', 70, 'CT-999001-14'] ,
['A-0382', 'LAT', 'GG-0065363', 10, 'CT-999001-15'] ,['A-0382', 'LAT', 'GG-0079653', 20, 'CT-999001-16'] ,
['A-0382', 'LAT', 'GG-0079665', 30, 'CT-999001-17'] ,['A-0386', null, 'GG-0113512', 40, 'CT-999001-18'] ,
['A-0386', null, 'GG-0113515', 50, 'CT-999001-19'] ,['A-0386', null, 'GG-0113518', 60, 'CT-999001-20'] ,
['A-0386', null, 'GG-0030282', 70, 'CT-999001-21'] ,['A-0386', null, 'GG-0030295', 80, 'CT-999001-22'] ,
['A-0386', null, 'GG-0030296', 90, 'CT-999001-23']];
const items_master =[
['GG-1169268', 'T/L 401 #12', 0, 0, null, 'undefined', 'undefined', 'undefined', 'undefined'],['GG-0069269', 'T/T 442 #13', 0.0, 0.0, 62000.0 ,'4W', 'TIRE', 'TIRE-T', 'HS.C000160'],
['GG-0049451', 'T/L O43 #15', 0.0, 0.0, 31000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],['GG-0049454', 'T/L O45 #11', 21.0, 71.0, 16000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000167'],
['GG-0049455', 'T/L A442 #16.5', 0.0, 0.0, 19000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],['GG-0065355', 'T/L 9.15 (EU)', 78.4, 78.4, 11000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],
['GG-0065357', 'T/L 11-15 (EU)', 0.0, 0.0, 17000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],['GG-0065363', 'T/L 11L-15 I', 0.0, 0.0, 19000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],
['GG-0079653', 'T/T 5-15 EU', 0.0, 0.0, 6000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],['GG-0079665', 'T/T 7.50-20', 0.0, 0.0, 17000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],
['GG-0113512', 'T/L 8.75-16.5 #902 ', 21.0, 71.0, 16000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000167'],['GG-0113515', 'T/L 9.50-16.5 #902', 0.0, 0.0, 19000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],
['GG-0113518', 'T/L 12-16.5 #905', 0.0, 0.0, 31000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],['GG-0030282', 'PEU 235/70 R16 EU', 24.0, 73.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0030295', 'PEU 265/70 R16', 26.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0030296', 'PEU 265/70 R17', 28.0, 81.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0030297', 'PEU 245/65 R17', 25.0, 75.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0030298', 'PLT 215/85 R16 (EU)', 21.6, 77.2, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000155'],
['GG-0030299', 'PLT 235/85 R16 (EU)', 24.0, 80.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0030300', 'PLT 225/75 R16', 23.0, 75.0, 15000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000155'],
['GG-0030301', 'PLT 245/75 R16', 24.5, 77.0, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0030302', 'PLT 265/75 R16 #EU', 24.0, 81.0, 21000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],
['GG-0031180', 'PLT 245/70 R16 #EU', 24.5, 74.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0031181', 'PLT 255/70 R16 #EU', 25.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0031182', 'PVT 245/70 R17 #EU', 24.8, 77.6, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0031183', 'PVT 265/65 R17', 25.5, 77.5, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036641', 'PVT 265/65 R17 #EU', 25.5, 77.5, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036642', 'PVT 255/70 R16', 25.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036643', 'PVT 245/70 R17', 24.8, 77.6, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036644', 'PVT 245/70 R16', 24.5, 74.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036915', 'PVT 245/65 R17', 24.0, 74.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036916', 'PVT 265/70 R17', 28.0, 81.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036917', 'PVT 265/70 R16', 26.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036918', 'PVT 235/70 R16', 24.0, 73.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0039658', 'PVT 255/65 R18', 26.0, 78.9, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0039659', 'PVT 235/65 R17', 24.0, 74.0, 15000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0039660', 'PVT 225/65 R17', 22.0, 72.5, 14000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0039661', 'PLT 275/70 R18', 25.0, 81.0, 23000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],
['GG-0039662', 'PLT 265/70 R17', 28.0, 81.0, 20000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0039663', 'PLT 245/70 R17', 24.8, 77.6, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],
['GG-0047366', 'PLT 245/75 R17', 25.0, 81.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0050349', 'PLT 235/75 R15', 23.0, 73.0, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157']];
console.log(`history : %s`,history);
console.log(`items_master : %s`,items_master);
console.log(q);
if (('custid' in q) | ('regional' in q)) { // in case found custid or regional in object.keys
var existing_salehistory_db = [] ; // create sale history data list
if ('custid' in q) { //if custid existing in q object do this loop
// existing_salehistory_db = cust_db.filter(row => row[0]==q['custid'])
existing_salehistory_db = history.filter(row => (row[0]==q['custid']) | (row[1]==q['regional'] ));
}
const c_history = existing_salehistory_db.filter(row => row[0]== q.custid);
// create customer order items history
const c_history_items = c_history.map(r => r[2]);
console.log(c_history_items);
const c_map_tbl = new Map(c_history.map(detail => [detail[2], [detail[3],detail[4] ] ] ) );
var offer_lst = items_master.map(row =>
[ q.custid,
q.regional,
row[0],
row[1],
row[3],
row[4],
row[5],
row[6],
row[7],
row[8],
c_history_items.includes(row[0]) ? c_map_tbl.get(row[0])[0] : null ,
c_history_items.includes(row[0]) ? c_map_tbl.get(row[0])[1] : null
]
)
console.log(`offer list : %s` , offer_lst);
}
else {
console.log('has not customer id');
}
}
คราวนี้ตัว offer_lst ก็ได้ข้อมูลออกมา แต่การ หา Items ที่ซ้ำกันในทั้ง 2 อะเรย์ นอกจากวนลูปทีละตัว แล้ว push array ใหม่ มีทางอื่นไหมครับ?
ขอบคุณครับ
javascript : การทำ intersection ของ array ทำแบบไม่ต้องวนลูปทีละค่าไหมครับ? อยากทำแบบ inner join ของ dbms หนะครับ
function createoffer_items(q ={'custid':'A-0010', 'regional':'EU'}){
const history = [['A-0010', 'MOD', 'GG-1169268', 10, 'CT-999001-1'] ,['A-0010', 'MOD', 'GG-999002', 20, 'CT-999001-2'] ,
['A-0010', 'MOD', 'GG-999003', 30, 'CT-999001-3'] ,['A-0010', 'MOD', 'GG-999002', 40, 'CT-999001-4'] ,
['A-0022', 'MOD', 'GG-999066', 50, 'CT-999001-5'] ,['A-0022', 'MOD', 'GG-0019149', 60, 'CT-999001-6'] ,
['A-0022', 'MOD', 'GG-0027943', 70, 'CT-999001-7'] ,['A-0370', 'EU', 'GG-0069268', 10, 'CT-999001-8'] ,
['A-0370', 'EU', 'GG-0069269', 20, 'CT-999001-9'] ,['A-0370', 'EU', 'GG-0049451', 30, 'CT-999001-10'] ,
['A-0372', 'AO', 'GG-0049454', 40, 'CT-999001-11'] ,['A-0372', 'AO', 'GG-0049455', 50, 'CT-999001-12'] ,
['A-0372', 'AO', 'GG-0065355', 60, 'CT-999001-13'] ,['A-0382', 'LAT', 'GG-0049455', 70, 'CT-999001-14'] ,
['A-0382', 'LAT', 'GG-0065363', 10, 'CT-999001-15'] ,['A-0382', 'LAT', 'GG-0079653', 20, 'CT-999001-16'] ,
['A-0382', 'LAT', 'GG-0079665', 30, 'CT-999001-17'] ,['A-0386', null, 'GG-0113512', 40, 'CT-999001-18'] ,
['A-0386', null, 'GG-0113515', 50, 'CT-999001-19'] ,['A-0386', null, 'GG-0113518', 60, 'CT-999001-20'] ,
['A-0386', null, 'GG-0030282', 70, 'CT-999001-21'] ,['A-0386', null, 'GG-0030295', 80, 'CT-999001-22'] ,
['A-0386', null, 'GG-0030296', 90, 'CT-999001-23']];
const items_master =[
['GG-1169268', 'T/L 401 #12', 0, 0, null, 'undefined', 'undefined', 'undefined', 'undefined'],['GG-0069269', 'T/T 442 #13', 0.0, 0.0, 62000.0 ,'4W', 'TIRE', 'TIRE-T', 'HS.C000160'],
['GG-0049451', 'T/L O43 #15', 0.0, 0.0, 31000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],['GG-0049454', 'T/L O45 #11', 21.0, 71.0, 16000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000167'],
['GG-0049455', 'T/L A442 #16.5', 0.0, 0.0, 19000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],['GG-0065355', 'T/L 9.15 (EU)', 78.4, 78.4, 11000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],
['GG-0065357', 'T/L 11-15 (EU)', 0.0, 0.0, 17000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],['GG-0065363', 'T/L 11L-15 I', 0.0, 0.0, 19000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],
['GG-0079653', 'T/T 5-15 EU', 0.0, 0.0, 6000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],['GG-0079665', 'T/T 7.50-20', 0.0, 0.0, 17000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000035'],
['GG-0113512', 'T/L 8.75-16.5 #902 ', 21.0, 71.0, 16000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000167'],['GG-0113515', 'T/L 9.50-16.5 #902', 0.0, 0.0, 19000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],
['GG-0113518', 'T/L 12-16.5 #905', 0.0, 0.0, 31000.0 ,'4W', 'TIRE', 'TIRE-B', 'HS.C000143'],['GG-0030282', 'PEU 235/70 R16 EU', 24.0, 73.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0030295', 'PEU 265/70 R16', 26.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0030296', 'PEU 265/70 R17', 28.0, 81.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0030297', 'PEU 245/65 R17', 25.0, 75.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0030298', 'PLT 215/85 R16 (EU)', 21.6, 77.2, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000155'],
['GG-0030299', 'PLT 235/85 R16 (EU)', 24.0, 80.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0030300', 'PLT 225/75 R16', 23.0, 75.0, 15000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000155'],
['GG-0030301', 'PLT 245/75 R16', 24.5, 77.0, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0030302', 'PLT 265/75 R16 #EU', 24.0, 81.0, 21000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],
['GG-0031180', 'PLT 245/70 R16 #EU', 24.5, 74.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0031181', 'PLT 255/70 R16 #EU', 25.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0031182', 'PVT 245/70 R17 #EU', 24.8, 77.6, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0031183', 'PVT 265/65 R17', 25.5, 77.5, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036641', 'PVT 265/65 R17 #EU', 25.5, 77.5, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036642', 'PVT 255/70 R16', 25.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036643', 'PVT 245/70 R17', 24.8, 77.6, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036644', 'PVT 245/70 R16', 24.5, 74.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036915', 'PVT 245/65 R17', 24.0, 74.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036916', 'PVT 265/70 R17', 28.0, 81.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0036917', 'PVT 265/70 R16', 26.0, 76.5, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0036918', 'PVT 235/70 R16', 24.0, 73.0, 16000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0039658', 'PVT 255/65 R18', 26.0, 78.9, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0039659', 'PVT 235/65 R17', 24.0, 74.0, 15000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],
['GG-0039660', 'PVT 225/65 R17', 22.0, 72.5, 14000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000003'],['GG-0039661', 'PLT 275/70 R18', 25.0, 81.0, 23000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],
['GG-0039662', 'PLT 265/70 R17', 28.0, 81.0, 20000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0039663', 'PLT 245/70 R17', 24.8, 77.6, 18000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],
['GG-0047366', 'PLT 245/75 R17', 25.0, 81.0, 19000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157'],['GG-0050349', 'PLT 235/75 R15', 23.0, 73.0, 17000.0 ,'4W', 'TIRE', 'TIRE-R', 'HS.C000157']];
console.log(`history : %s`,history);
console.log(`items_master : %s`,items_master);
console.log(q);
if (('custid' in q) | ('regional' in q)) { // in case found custid or regional in object.keys
var existing_salehistory_db = [] ; // create sale history data list
if ('custid' in q) { //if custid existing in q object do this loop
// existing_salehistory_db = cust_db.filter(row => row[0]==q['custid'])
existing_salehistory_db = history.filter(row => (row[0]==q['custid']) | (row[1]==q['regional'] ));
}
const c_history = existing_salehistory_db.filter(row => row[0]== q.custid);
// create customer order items history
const c_history_items = c_history.map(r => r[2]);
console.log(c_history_items);
const c_map_tbl = new Map(c_history.map(detail => [detail[2], [detail[3],detail[4] ] ] ) );
var offer_lst = items_master.map(row =>
[ q.custid,
q.regional,
row[0],
row[1],
row[3],
row[4],
row[5],
row[6],
row[7],
row[8],
c_history_items.includes(row[0]) ? c_map_tbl.get(row[0])[0] : null ,
c_history_items.includes(row[0]) ? c_map_tbl.get(row[0])[1] : null
]
)
console.log(`offer list : %s` , offer_lst);
}
else {
console.log('has not customer id');
}
}
คราวนี้ตัว offer_lst ก็ได้ข้อมูลออกมา แต่การ หา Items ที่ซ้ำกันในทั้ง 2 อะเรย์ นอกจากวนลูปทีละตัว แล้ว push array ใหม่ มีทางอื่นไหมครับ?
ขอบคุณครับ