【发布时间】:2019-11-09 01:05:59
【问题描述】:
我有多个来自 JSON 响应的同名数据。我正在寻找一个函数来过滤掉我的选项列表中的同名数据。
const select = document.getElementById('brand');
/**===============Fetch requests=========== */
fetch("http://makeup-api.herokuapp.com/api/v1/products.json")
.then((resp) => resp.json())
.then(function(data) {
let products = data;
return products.map(function(product) {
console.log(product.brand);
})
})
.catch(function(error) {
console.log(error);
});
fetch("http://makeup-api.herokuapp.com/api/v1/products.json")
.then((resp) => resp.json())
.then((resp) => generateOptions(resp))
/*===========Helper Functions====================== */
function generateOptions(data) {
let products = data;
const options = products.map(item => `
<option value='${item.brand}'>${item.brand}</option>
`).join('');
select.innerHTML = options;
}
【问题讨论】:
-
响应数据是什么样的?
标签: javascript json reactjs api fetch