【问题标题】:vue, array/item map in order to flatten into one level per idvue,数组/项目映射,以便每个 id 扁平化为一个级别
【发布时间】:2020-03-04 00:34:39
【问题描述】:

在 Vue 中,我收到了来自 axios 的响应,我将其转储到我的控制台中。它在数据方面看起来不错,但是由于注释类型不同,返回的数据对象每个 item_id 有多个行。这导致我无法在前端每行使用一条记录。

我需要找到正确的方法,让每个 item_id 只有一个主级别,并将 cmets 嵌套在该级别下的一个级别中,这样即使一条记录有 5 种不同的评论/文本类型,它也只是一个 id 的一部分响应记录(我可以在前端使用比较来在某些地方显示某些评论/文本类型)。

在 this.events 的 console.log 中,我得到:

0:
  item_id: 12,
  item_text_type: comment,
  item_text: "this has been updated",
  date: "2020/03/03"

1:
  item_id: 12,
  item_text_type: title,
  item_text: "new entry",
  date: "2020/03/03"

2:
  item_id: 13,
  item_text_type: comment,
  item_text: "this has been updated",
  date: "2020/03/03"

3:
  item_id: 13,
  item_text_type: title,
  item_text: "2nd Entry",
  date: "2020/03/03"

我想让它们变成更像这样的结构:

0:
  item_id: 12,
  date: "2020/03/03",
  comments:
    item_text_type: comment,
    item_text: "this has been updated"
    item_text_type: title,
    item_text: "new entry"

0:
  item_id: 13,
  date: "2020/03/03"
  comments:
    item_text_type: comment,
    item_text: "this has been updated"
    item_text_type: title,
    item_text: "2nd entry"

所以基本上,每个 item_id 只有一条记录,并将 cmets 组合成一个子数组或类似的东西。其他信息(例如日期)将在 id 级别。

我怎样才能改变这个来达到这个结果

fetchTasks() {
  axios.get('/home')
  .then(response => {
    //handle success

    this.events = response.data.map(({item_comment, ...item}) => ({
      ...item,
      title: item_comment
    }));

    console.log(this.events);

  })
  .catch(function(error) {
    // handle error
    console.log(error)
  })
  .finally(function() {

  })
},

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    更新

    现在这包括与 item_idcmets 相关值相关的任何其他数据然而只有遇到的任何冲突额外数据中的最后一个对于任何特定的 item 都会被保留。

    您也不能有任何名为comments 的额外数据。如果是这样,它将被丢弃。


    似乎是一个非常直接的数据转换。您需要将数组缩减为某种类型的 map(例如普通对象或 Map),由带有 typeitem_id 属性键控em>text 属性添加为数组中的对象。

    从那里,您可以通过将键和值分别映射到 item_idcomments 来将该 map 转换为所需的对象数组。

    类似的东西

    const res = [
      {item_id: 12, item_text_type: 'comment', item_text: "this has been updated", date: '2020/03/03' },
      {item_id: 12, item_text_type: 'title', item_text: "new entry", date: '2020/03/04' },
      {item_id: 13, item_text_type: 'comment', item_text: "this has been updated", date: '2020/03/05' },
      {item_id: 13, item_text_type: 'title', item_text: "2nd entry", date: '2020/03/06' }
    ]
    
    const dataMap = res.reduce((map, { item_id, item_text_type, item_text, ...extra }) => {
      let { comments, ...item } = map.has(item_id) ? map.get(item_id) : { comments: [] }
      comments.push({ item_text_type, item_text })
      return map.set(item_id, { ...item, ...extra, comments })
    }, new Map())
        
    const transformed = Array.from(dataMap, ([ item_id, item ]) => ({
      item_id,
      ...item
    }))
    
    console.info(transformed)
    .as-console-wrapper { max-height: none !important; height: 100%; }

    【讨论】:

    • 完美,我想这正是我在这里寻找的。我会继续玩这个,谢谢!
    • 我试过了,它奏效了,但我也有尾随日期(见我上面的编辑),它们没有显示在转换后的信息中。是否还需要推送 cmets 之外的任何信息?如果我想让每个项目都有 ID、日期、类别,然后是 cmets 数组,我想知道是否需要推送每个值
    • 其他任何东西都将在顶级 ID 中,只有 cmets 会在数组中。我只是在想,因为我将来会添加更多内容。为了安全起见,我将编辑我的问题
    • @TomN。这个额外的要求显着改变了你的问题,所以我建议你编辑它而不是我的答案,因为不清楚你现在想要什么
    • 我编辑了我的问题。这不是一个巨大的变化,我只是确保是否将任何内容添加到父记录并且只需要存在于 id 级别,我将如何实现它
    猜你喜欢
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2018-04-02
    • 1970-01-01
    • 2019-04-23
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多