한 줄의 코드로 객체 배열에서 테이블 생성

3734 단어 htmljavascript
<html lang=de>
  <meta charset=UTF-8>
  <title>Document</title>
 <link rel="stylesheet" 
href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css">
<body class=container>
  <h1>Array of objects to table</h1>
<script>
  let cities = [
  {"city": "Berlin","country": "Germany"},
  {"city": "Paris","country": "France"}]
  document.querySelector('h1').insertAdjacentHTML('afterend',
  `<table><thead><tr><th>
   ${Object.keys(cities[0]).join('<th>')}
  </thead><tbody><tr><td>${cities.map(e=>Object.values(e)
.join('<td>')).join('<tr><td>')}</table>`)
</script>

좋은 웹페이지 즐겨찾기