@Superdsa

Как сделать данную конкатенацию в js?

Как добавить for в конкатенацию js
content.append('<thead>\
                        <tr>\
                        <th scope="col">#</th>\
                            <th scope="col">First</th>\
                            <th scope="col">Last</th>\
                            <th scope="col">Handle</th>\
                            </tr>\
                            </thead>\
                            <tbody>\
                                '+ for (var i = 0; i < data.length; i++) { +'<tr id="'+ data[i].id + '" class="table-activate">\
                                    <th scope="col">' + data[i].id +'</th>\
                                    <th scope="col">' + data[i].name +'</th>\
                                    <th scope="col">' + data[i].token +'</th>\
                                    <th scope="col">' + data[i].owner_id +'</th>\
                                    </tr>\
                                     '+}

                    );
  • Вопрос задан
  • 81 просмотр
Решения вопроса 1
В JavaScript’е цикл for не возвращает значение. Вынесите цикл в функцию и вызывайте её.

Вот так, например:

const tableRow = ({id, name, token, owner_id}) => (`
    <tr id="${id}" class="table-activate">
        <td scope="col">${id}</td>
        <td scope="col">${name}</td>
        <td scope="col">${token}</td>
        <td scope="col">${owner_id}</td>
    </tr>
`);

const tableBody = data => data.reduce((body, elem) => (body + tableRow(elem)), '');

content.append(`
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">First</th>
            <th scope="col">Last</th>
            <th scope="col">Handle</th>
        </tr>
    </thead>
    <tbody>
        ${tableBody(data)}
    </tbody>
`);
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы