Показать сообщение отдельно
  #55 (permalink)  
Старый 07.12.2018, 14:02
Профессор
Отправить личное сообщение для Nexus Посмотреть профиль Найти все сообщения от Nexus
 
Регистрация: 04.12.2012
Сообщений: 3,775

Vlad777,я с laimas согласен, этим должен сервер заниматься.
new Promise(function(resolve, reject) {
    $.ajax({
        url: 'au.csv',
        dataType: 'text',
        success: resolve,
        error: reject
    });
}).then(function(response) {
    const CELL_DELIMITER = ',';

    return response.split(/\r?\n|\r/).map(function(row) {
        const cells = row.split(CELL_DELIMITER).map(function(value) {
            return (value || '').trim();
        });

        return {
            index: cells[0],
            fio: cells[1],
            inn: cells[2],
            score: cells[3] + ',' + cells[4],
            sro: cells[5]
        };
    });
}).then(function(data) {
    const getFirstChar = function(string) {
        return (string || '').substr(0, 1).toLowerCase();
    };

    const indexOfNames = {};
    data.forEach(function(person, key) {
        const firstChar = getFirstChar(person.fio);
        if (!firstChar)
            return;

        (indexOfNames[firstChar] || (indexOfNames[firstChar] = [])).push({
            name: person.fio.toLowerCase(),
            key: key
        });
    });

    return function(name) {
        name = name.toLowerCase();
        const firstChar = getFirstChar(name);
        if (!indexOfNames[firstChar])
            return [];

        const response = [];
        indexOfNames[firstChar].forEach(function(item) {
            if (item.name.indexOf(name) < 0)
                return;

            response.push(data[item.key]);
        });


        return response;
    };
}).then(function(search) {
    console.log(search('Прудников'));
});
Ответить с цитированием