Answer the question
In order to leave comments, you need to log in
How to find elements in an array and replace their values?
Hello! Faced with the most difficult task, which I have been solving for 6 hours. Help please...
So, we have a data.json file with the following content.
[{"input":"продаю машину","output":"ban"},{"input":"продаю коня","output":"moderation"},{"input":"продаю зебру","output":"normal"},{"input":"продаю льва","output":"normal"},{"input":"продаю льва","output":"normal"},{"input":"продаю осла"}]
let data = require('./data.json');
let input = "input";
let output = "output";
let text = message.text.toLowerCase();
let new_status = 'normal';
let data_find = data.filter(f => f.input.toLowerCase() === text);
if (data_find.length !== 0) {
let new_data = data.filter(function (f) {
return f.input.toLowerCase() !== text;
});
let data_replace = data_find.map(e => e[input].toLowerCase() === text ? ((e[output] = new_status), e) : e);
// объединяем массивы и записываем
new_data = new_data.concat(data_replace[0]);
let filepatch = './detection/AutoBan/data.json';
fs.outputJson(filepatch, new_data, err => {
if (err) return console.log(err);
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question