Quantcast
Channel: 連想配列タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 129

連想配列をJavaScriptで要素を簡易的に追加する

$
0
0

メモ用。
連想配列のに要素をforEach等で配置する

const arr = [
{name: 'aaa', image: "aaa.JPG", disc: 'text~'},
{name: "bbb", image: "bbb.JPG", disc: 'text~'},
];

function viewList(content, cls) {
const list = document.createElement('li');
const img = document.createElement('img');
if (cls === 'toImage') {
img.classList.add(${cls})
img.src = content;
} else {
list.classList.add(${cls});
list.textContent = content;
}
list.appendChild(img);
ul.appendChild(list);
}

arr.forEach(ar => {
viewList(ar.name, 'class名');
viewList(ar.image, 'class名');
viewList(ar.disc, 'class名');
})


Viewing all articles
Browse latest Browse all 129

Trending Articles