【问题标题】:Logical grouping in HTML for results pulled from objectHTML中的逻辑分组,用于从对象中提取的结果
【发布时间】:2022-01-12 00:48:36
【问题描述】:

我成功地从 API 遍历对象/数组,并且能够获取三个参数,作者/标题/描述,并将每个参数的结果放入它们自己的列表中。换句话说,数组中的所有作者都在html中放入一个列表中,所有标题放入一个列表中,所有描述放入一个列表中。

如何对结果进行逻辑分组,例如,对于每本书,将结果作者/标题/描述放在页面上的一个区域...(div?)?

const searchButton = document.getElementById('searchbtn')
const form = document.getElementById('myform')
const author = document.querySelector('.author')
const title = document.querySelector('.title')
const description = document.querySelector('.description')

document.addEventListener("DOMContentLoaded", () => {
    searchButton.addEventListener('click', e => {
        e.preventDefault()
        const inputSearch = document.getElementById('books').value
        const bookUrl = ('https://www.googleapis.com/books/v1/volumes?q=' + inputSearch)
        fetch(bookUrl)
        .then (res => res.json())
        .then (obj => {
            obj.items.forEach((book, index) => {
                const listAuthor = document.createElement('li')
                const listTitle = document.createElement('li')
                const listDescription = document.createElement('li')
                listAuthor.innerText = book.volumeInfo.authors
                listTitle.innerText = book.volumeInfo.title
                listDescription.innerText = book.volumeInfo.description

                author.appendChild(listAuthor)
                title.appendChild(listTitle)
                description.appendChild(listDescription)
            })
            form.reset()
        })
    })
})

【问题讨论】:

  • 如果不将您当前的代码视为minimal reproducible example,我们将无法帮助您,并简要说明您在执行此分组时遇到的困难,以及一些示例数据。请看How to Ask
  • 我在您发布此评论时添加了我的代码。我怎样才能更清楚地解释我的目标?
  • '对于每本书都将结果作者/标题/描述放入一个区域',所以您想要一个 div(书籍容器)并在该 div 中一一列出书籍?您需要实现什么样的分组?

    {作者}、{标题}、{Desc}

    ?只是在单个 div 中具有相应属性的大量书籍?
  • 我正在做一个非常简单的学生项目。我只需要将结果显示在一页上。我当前的代码从 API 获取信息并将其填充到每个类别的三个单独的列表中。我需要“第 1 本书,作者/标题/描述”,然后是“第 2 本书,作者/标题/描述”。我是初学者,不知道你提出的分组是什么意思。

标签: javascript html


【解决方案1】:

如果我误解了您的问题,请告诉我。

const booksArray = [
  {author: 'Eric Hoffer', title:'The Ordeal Of Change', desc: 'In times of change...' },
  {author: 'Eric Hoffer', title:'The True Believer', desc: 'Learners inherit the earth' },
  {author: 'Eric Hoffer', title:'The Passionate State Of Mind', desc: 'while the learned...' },
]


const groupBooks = (books) => {
  const bookContainer = document.createElement('section');

  books.forEach((book, index) => {
    // extract values
    const {author, title, desc} = book;

    // construct composite book article
    const bookDescription = `
          <article>
            <small>book ${index + 1} </small>
            <h2>Title: ${title}</div> 
            <h3>Author: ${author}</h3> 
            <p>Desc: ${desc}</p> 
          </article>
          `;

    // append it to the main container above
    bookContainer.append(bookDescription);
  })

  console.log(bookContainer);
  //to see the results...
  document.body.prepend(bookContainer);

}

groupBooks(booksArray);

【讨论】:

  • 谢谢您,我会尝试应用您在此处输入的内容,虽然我不知道该怎么做,我是一名学生,还没有这个窍门。跨度>
  • 确保将“适当的”数据输入函数。您可能需要自己调整参数......这仅供您参考。这是基于我上面创建的示例数组。
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多