katie hudson
katie hudson

Reputation: 2893

Comparing two arrays to retrieve values

I asked a similar question earlier but it was unclear. I am building an array. At the moment I am doing this

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

The output of the above is the following

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

You can see that the counts are currently manually set to 0. I then have a second array I retrieve from the server called dataArray. The contents of this are

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

What I am trying to do is is with myArray I produced above, I need to switch the 0's out with the correct counts found for the Category within dataArray. So essentially myArray should end up like this

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

How would I go about cross comparing the two arrays?

Thanks

Upvotes: 1

Views: 50

Answers (5)

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92417

Try

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

Upvotes: 0

bitznbytez
bitznbytez

Reputation: 106

Well I had a similar situation, I believe the following would get the results you are looking for. I think it is easy to read as well, although my variable names can be replaced of course.

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

Upvotes: -1

Kousher Alam
Kousher Alam

Reputation: 1055

I think your problem is you have an array

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

And another array from server

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

Now you need to produce

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

To archive this you need to do

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

Now your array1 will have your desire result.

Upvotes: 1

vzwick
vzwick

Reputation: 11044

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

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138267

From your server data, create a Map of category to count:

  const countOf = new Map();

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

Then you can go over your table, look up the count and replace it:

 const Count = 1, Category = 0;

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

Upvotes: 1

Related Questions