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

Using spread operator to update dictionary value which is an object array

我正在尝试将 API 有效载荷保存到字典中。 API 数据模型具有属性“category”、“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)
        }
    })
}