shura812
@shura812
I'm only study html,css!

Как достать три раза по индексу значения из массива?

const info = {
    title: 'Hello!!!',
    graduatesCount: 2000,
    areYouChampion: true,
    technologies: ['Front', 'Back', 'Devops']
}
let techSelect = document.createElement('select');
let render = document.createElement('option');
render.append(info.technologies);
techSelect.append(render);
document.body.append(techSelect);
  • Вопрос задан
  • 138 просмотров
Решения вопроса 1
delphinpro
@delphinpro Куратор тега JavaScript
frontend developer
1. Классика, цикл for {}
const info = {
    title: 'Hello!!!',
    graduatesCount: 2000,
    areYouChampion: true,
    technologies: ['Front', 'Back', 'Devops']
}
let techSelect = document.createElement('select');
for (let i = 0; i < info.technologies.length; i++) {
    let render = document.createElement('option');
    render.append(info.technologies[i]);
    techSelect.append(render);
}
document.body.append(techSelect);


2. Цикл for..in
let techSelect = document.createElement('select');
for (let i in info.technologies) {
    let render = document.createElement('option');
    render.append(info.technologies[i]);
    techSelect.append(render);
}
document.body.append(techSelect);


3. Цикл for..of
let techSelect = document.createElement('select');
for (let i of info.technologies) {
    let render = document.createElement('option');
    render.append(i);
    techSelect.append(render);
}
document.body.append(techSelect);


4. Метод массива forEach()
let techSelect = document.createElement('select');
info.technologies.forEach(i => {
    let render = document.createElement('option');
    render.append(i);
    techSelect.append(render);
});
document.body.append(techSelect);


5. Метод массива reduce()
let techSelect = document.createElement('select');
techSelect.append(info.technologies.reduce((acc, i) => acc+`<option>${i}</option>`, ''));
document.body.append(techSelect);
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

Войти через центр авторизации
Похожие вопросы
01 мая 2024, в 02:11
5000 руб./за проект
01 мая 2024, в 00:29
2000 руб./за проект
01 мая 2024, в 00:20
15000 руб./за проект