簡體   English   中英

比較兩個數組以檢索值

[英]Comparing two arrays to retrieve values

我之前曾問過類似的問題,但目前尚不清楚。 我正在建立一個數組。 目前,我正在這樣做

let myArray = [];
const header = ["Category", "Count", { role: 'style' }];
const categories = ["cat1", "cat2", "cat3", "cat4"];
const colors = ["red", "blue", "silver", "yellow"];
myArray.push(header);
categories.forEach((x,i) => {
    myArray.push([x, 0, colors[i]]);
});
console.log(JSON.stringify(myArray));

上面的輸出如下

[
    ["Category","Count",{"role":"style"}],
    ["cat1",0,"red"],
    ["cat2",0,"blue"],
    ["cat3",0,"silver"],
    ["cat4",0,"yellow"]
]

您可以看到當前將計數手動設置為0。然后從服務器中檢索了另一個數組dataArray 這個的內容是

[
    {"Category":"cat3","Count":59},
    {"Category":"cat1","Count":109},
    {"Category":"cat2","Count":120},
    {"Category":"cat4","Count":57}
]

我要嘗試的是使用上面產生的myArray ,我需要使用為dataArray的Category找到的正確計數來切換0。 所以本質上myArray應該像這樣結束

[
    ["Category","Count",{"role":"style"}],
    ["cat1",109,"red"],
    ["cat2",120,"blue"],
    ["cat3",59,"silver"],
    ["cat4",57,"yellow"]
]

我將如何交叉比較兩個數組?

謝謝

根據您的服務器數據,創建要分類的類別映射:

  const countOf = new Map();

  for(const { Category, Count } of serverData)
    countOf.set(Category, Count);

然后,您可以翻閱表格,查找計數並替換它:

 const Count = 1, Category = 0;

 for(const row of table.slice(1))
   row[Count] = countOf.get(row[Category]);

Quick'n'dirty:

 let myArray = []; const header = ["Category", "Count", { role: 'style' }]; const categories = ["cat1", "cat2", "cat3", "cat4"]; const colors = ["red", "blue", "silver", "yellow"]; myArray.push(header); categories.forEach((x,i) => { myArray.push([x, 0, colors[i]]); }); const dataArray = [ {"Category":"cat3","Count":59}, {"Category":"cat1","Count":109}, {"Category":"cat2","Count":120}, {"Category":"cat4","Count":57} ]; dataArray.forEach(e => { const elementToUpdate = myArray.find(x => x[0] === e.Category); elementToUpdate && (elementToUpdate[1] = e.Count); }); console.log(myArray); 

我想你的問題是你有一個數組

var array1 = [
    ["Category","Count",{"role":"style"}],
    ["cat1",0,"red"],
    ["cat2",0,"blue"],
    ["cat3",0,"silver"],
    ["cat4",0,"yellow"]
]

還有來自服務器的另一個陣列

var serverarr = [
    {"Category":"cat3","Count":59},
    {"Category":"cat1","Count":109},
    {"Category":"cat2","Count":120},
    {"Category":"cat4","Count":57}
]

現在你需要生產

[
    ["Category","Count",{"role":"style"}],
    ["cat1",109,"red"],
    ["cat2",120,"blue"],
    ["cat3",59,"silver"],
    ["cat4",57,"yellow"]
]

要對此進行存檔,您需要執行

serverarr.forEach(item =>{
  array1.forEach(data =>{
    if(data[0] === item.Category){
     data[1] = item.Count; 
    }   
  });
});

現在,您的array1將具有您想要的結果。

嘗試

let cat={};
dataArray.forEach(x=> cat[x.Category]=x.Count);
data.forEach((x,i)=> i>0? x[1]=cat[x[0]]:0 );

 let data= [ ["Category","Count",{"role":"style"}], ["cat1",0,"red"], ["cat2",0,"blue"], ["cat3",0,"silver"], ["cat4",0,"yellow"] ] let dataArray = [ {"Category":"cat3","Count":59}, {"Category":"cat1","Count":109}, {"Category":"cat2","Count":120}, {"Category":"cat4","Count":57} ] let cat={}; dataArray.forEach(x=> cat[x.Category]=x.Count); data.forEach((x,i)=> i>0? x[1]=cat[x[0]]:0 ); console.log(data); 

好吧,我也遇到過類似的情況,我相信以下內容可以得到您想要的結果。 盡管我的變量名當然可以替換,但我認為它也很容易閱讀。

dataArray.forEach((x) => {
    myArray.forEach((z) => {
        if (z.includes(x.Category)) {
            z.splice(z.indexOf('0'), 0, x.Count);
        }
    });
});

暫無
暫無

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

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