使用扩展运算符更新字典值,该字典值为对象数组



我试图将API有效负载保存到字典中。API数据模型具有"类别"、"id"属性。和"name".

let apiPayload = [];
(async () => {
let fetchValue = await
fetch('https://asia-southeast1-citric-pager-319712.cloudfunctions.net/gcp-simple-api').then(response => {
if (response.ok) {
return response.json();
}
throw response;
}).then(data => {
return data;
}).catch(error => console.log(error));
console.log('fetchValue', fetchValue);
let dictionary = Object.assign({}, ...fetchValue.map((x) => ({
[x.category]: [x]
})));
console.log('dictionary', dictionary);
})();

如何在字典中添加新的类别对象,使其按类别与类别对象排序,例如

HTML: [{
category: "HTML",
id: "blog-post",
name: "Blog Post"
}, {...}, {...}],
JavaScript: [{
category: "JavaScript",
id: "curry",
name: "Curry"}, {...}, {...}]

给你:

async function sample() {
let categories = {}
let fetchValue = [] //Array of object, plug your fetch data here
fetchValue.forEach(e=>{
if(!categories[e.category]){
categories[e.category]= [e]
}else{
categories[e.category].push(e)
}
})
}

相关内容

  • 没有找到相关文章

最新更新