How to add Numbers In Arrays format if we have a word in 3 files using nodejs

31 views Asked by At

I want to ask a question, what is it, I have three files, if there is a Word COMMON in those files then it should be printed like this [1,2 3], otherwise, if there is a word in 1 and 2 then it should be printed like this [1 2] , I Tried to PUSH ARRAY but it's not happening Here Is My Code:

 let Page1data = filtered.map((val) => {
      let data = {};
      if (Page1.includes(val)) {
        data[val] = ["1"];
      }
    
      if (Page2.includes(val)) {
        data[val] = ["2"];
      }
      if (Page3.includes(val)) {
        data[val] = ["3"];
      }
      return data;
    });
    
    console.log(Page1data);
1

There are 1 answers

0
M3VRIX On

If I get it right, the problem is with your declaration. .push() is for arrays not for objects. You have declared your data variable as an object.

You should use:

let data = [];

instead of

let data = {};

So it's going to look like this:

let data = [];
  if (Page1.includes(val)) {
    data.push("1");
  }

etc...