簡體   English   中英

從 javascript 中的另一個數組過濾結果

[英]filter results from another array in javascript

所以我有兩個這樣的 arrays。

let emailList = [ [ 'OM Email', 'Team/Location' ],
                  [ 'email2@email.com', 'Addison' ],
                  [ 'email1@email.com', 'SouthArlington' ]]

let data = [ [ 'Addison',13373,'Addison','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
              73 ],
             [ 'SouthArlington',13373,'SouthArlington','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
              73 ],

[ 'Addison',13374,'Addison','Office Team3',5,'Jordan M','DFW','General',2,1,0,31,63, 73 ] ]

id 喜歡過濾data數組,看看它是否從emailList中找到一個位置。 data中的 position 在2處)如果它確實從數據中找到了一個位置,彈出它並每次為每個位置創建一個新數組。

我想做的是通過 google-sheets 過濾特定列何時等於某物。 這是目前有效的,那就是來自data數組的信息。 我想要做的是從 emailList 向每個辦公室發送通知以及它們各自的行。

這就是我從 google-sheet 文檔中過濾data的方式

  let data = thisSpreadsheet.filter(function (row,index) {
      return row[11] >= 30 
  });

如果我再次過濾數據,我會得到結果,但每個匹配的辦公室都需要它。

let locationList  = data.filter(function(location){

    return location[2] === emailList[1][1]

})

預期的:

    let Addison = [[ 'Addison',13373,'Addison','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
                  73 ],
[ 'Addison',13374,'Addison','Office Team3',5,'Jordan M','DFW','General',2,1,0,31,63,
                  73 ]]

    let SouthArlington = [ 'SouthArlington',13373,'SouthArlington','Office Team',4,'Jordan M','DFW','General',2,1,0,31,63,
                  73 ]

您需要的過濾器:

let locationList  = data.filter(function(location){
    return emailList.some(office => office[1] === location[2]);
})

在這里,我們使用Array.some()並檢查每個datalocation是否包含在數組中的任何辦公室中。

另一種選擇是創建一個 object:

let result = {};
let locationList  = data.filter(function(location){
    return emailList.some(office => office[1] === location[2]);
})
locationList.forEach(loc => result[loc[2]] = loc);

然后result包含: {Addison: Array(14), SouthArlington: Array(14)}

您可以使用Array.prototype.reduce創建一個 map,它根據位置將數據存儲為 map 鍵。 請參閱以下評論:

 let data = [ ['Addison', 13373, 'Addison', 'Office Team', 4, 'Jordan M', 'DFW', 'General', 2, 1, 0, 31, 63, 73], ['SouthArlington', 13373, 'SouthArlington', 'Office Team ', 4, ' Jordan M ', ' DFW ', ' General ', 2, 1, 0, 31, 63, 73] ]; // Call Array.prototype.reduce on data const emailByLocation = data.reduce((map, arr) => { const [location, ...rest] = arr; if (map.get(location) == null) { // Add a new map entry each time we encounter a new location name in // data. The key of the entry will be the location name (which is in // the first element of arr parameter) and set the value to an array // that contains 1 item (ie: arr). map.set(location, [arr]); } else { // Add additional to the map entry array value for this location. map.get(location).push(arr); } return map; }, new Map()); // Iterate over the map entries to see the values emailByLocation.forEach((value, key) => { console.log(`[${key}] = ${value}`); }); // Since emailByLocation is a Map, you can take advantage of that // and get the data for each location simply by passing the key (ie: the location name). console.log('Here is the info for Addison:' + emailByLocation.get("Addison")); console.log('Here is the info for SouthArlington:' + emailByLocation.get("SouthArlington"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM