比较两个数组以检索值

时间:2019-06-07 19:35:46

标签: javascript arrays

我早些时候问过类似的问题,但目前尚不清楚。我正在建立一个数组。目前,我正在这样做

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"]
]

我将如何交叉比较两个数组?

谢谢

5 个答案:

答案 0 :(得分:1)

根据您的服务器数据,创建要计数的类别地图:

  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]);

答案 1 :(得分:1)

快速肮脏:

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);

答案 2 :(得分:1)

我认为您的问题是您有一个数组

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将得到您想要的结果。

答案 3 :(得分:0)

尝试

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);

答案 4 :(得分:-1)

好吧,我也遇到类似的情况,我相信以下内容将为您提供所需的结果。我认为它也很容易阅读,尽管我的变量名当然可以替换。

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