简体   繁体   中英

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

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM