JSFiddle - React, Tailwind, and code Playground
by isqua
JavaScript
// Паттерн для нахождения подходящих ключей
var attachReg = /^attach(\d+)$/;
function processAttach(message) {
return function iterator(item, key, callback) {
var matches = attachReg.exec(key);
// Если не попадает под паттер attachN, пропускаем
if ( ! matches) {
return callback();
}
var i = matches[1];
var type = message.attach['attach' + i + '_type'];
var split = item.split('_');
message.attach[type] = message.attach[type] || [];
message.attach[type].push({
/* Id прикрепления */
id: parseInt(split[1]),
/* Id стенки/пользователя/ */
owner: split[0].toString(),
/* Для быстрого прикрепления */
get: type + item
});
delete message.attach[key];
delete message.attach['attach' + i + '_type'];
};
}
// Идём по всем ключам message.attach
async.forEachOf(message.attach, processAttach(message), function(err) {
if (err) {
console.log(err);
}
});