Home > Mobile >  filtering an array issue
filtering an array issue

Time:09-06

I'm importing an array into a module, and adding and removing items from that array. when I give a push, it adds the item to the array globally, so much so that if I use that same array in another module, it will include this item that I pushed. but when I try to filter, with that same array getting itself with the filter, it only removes in that specific module. How can I make it modify globally?

let { ignore } = require('../utils/handleIgnore');
const questions = require('./quesiton');

const AgendarCollector = async (client, message) => {
    ignore.push(message.from);
    let counter = 0;
    const filter = (m) => m.from === message.from;
    const collector = client.createMessageCollector(message.from, filter, {
        max: 4,
        time: 1000 * 60,
    });
    await client.sendText(message.from, questions[counter  ]);

    collector.on('start', () => {});
    await collector.on('collect', async (m) => {
        if (m) {
            if (counter < questions.length) {
                await client.sendText(message.from, questions[counter  ]);
            }
        }
    });
    await collector.on('end', async (all) => {
        ignore = ignore.filter((ignored) => ignored !== message.from);
        console.log(ignore);
        const finished = [];
        if (all.size < questions) {
            console.log('não terminado');
        }
        await all.forEach((one) => finished.push(` ${one.content}`));
        await client.sendText(message.from, `${finished}.\nConfirma?`);
    });
};

module.exports = AgendarCollector;

see, in this code, import the ignore array and i push an item to then when the code starts and remove when its end. but the item continues when I check that same array in another module. I tried to change this array ignore by using functions inside this module but still not working

let ignore = [];

const addIgnore = (message) => {
    ignore.push(message.from);
};
const removeIgnore = (message) => {
    ignore = ignore.filter((ignored) => ignored !== message.from);
    console.log(ignore);
};
console.log(ignore);
module.exports = { ignore, addIgnore, removeIgnore };

CodePudding user response:

You are using the variables for import and export and hence cought up with issues.

Instead, make use of getters.

Write a function which will return the array of ignore. something like this:

const getIgnoredList = () => {
    return ignore;
};

and in your first code, import getIgnoredList and replace ignore with getIgnoredList()

Explanation :

Whenever we import the variables only the value at that particular time will be imported and there will not be any data binding. Hence there won't be any change in the data even though you think you are updating the actual data.

Hope this helps you!! Please comment if you get any errors or stuck with the above one.

CodePudding user response:

When you use require(...) statement it's executed only once. Hence when you try to access the property it gives the same value everytime. Instead you should use getters

let data = {
   ignore : [],
   get getIgnore() {
     return this.ignore
   }
}
module.export = {
  getIgnore: data.getIgnore,
}

Then wherever you want to access ignore do

var {getIgnore}= require('FILE_NAME')

Now: console.log(getIgnore) will invoke the getter and give you current value of ignore

Using getters will allow you to access particular variables from other modules but if you want to make changes in value of those variables from other module you have to use setter.

More about getters here

More about setters here

  • Related