【发布时间】:2022-08-14 00:54:17
【问题描述】:
我下面的代码是获取特定产品类型的所有产品,并且该数组包含我想在不同函数中使用的数据。我将如何将获取的数据存储到一个可以在不同函数中使用的变量中(我需要添加一个点击事件并过滤以在其他地方显示来自该数组的其他信息)
const displayProduct = (event) => {
const info =document.getElementById(\'info\');
const ul = document.getElementById(\'makeup-list\');
ul.innerHTML = \" \";
fetch(`https://makeup-api.herokuapp.com/api/v1/products.json/?product_type=${event.target.value}`)
.then(res => res.json())
.then(data => {
data.forEach(data => {
info.innerHTML += `<li><a href=\"#\" data-id=>${data.name}</a></li>`;
});
标签: javascript arrays api global-variables fetch-api