鍍金池/ 問答/HTML/ 關(guān)于一個(gè)js數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換問題

關(guān)于一個(gè)js數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換問題

有這么一個(gè)評論場景,id是惟一的,answerId和id是對應(yīng)的,answerId是回復(fù)id的人,需要把COMMENT_MOCK_DATA_ORIGIN 數(shù)據(jù)轉(zhuǎn)化為result數(shù)據(jù)結(jié)構(gòu),相當(dāng)于多了一個(gè)字段answerArr為一個(gè)回復(fù)數(shù)組。

var COMMENT_MOCK_DATA_ORIGIN = [
    {
      "id": 52,
      "a_id": 205,
      "user": "用戶3",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303716",
      answerId:''
    },
    {
      "id": 54,
      "a_id": 205,
      "user": "用戶5",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303716",
      answerId: 52
    },
    {
      "id": 55,
      "a_id": 205,
      "user": "用戶5",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303716",
      answerId: 52
    },
    {
      "id": 51,
      "a_id": 205,
      "user": "用戶2",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303713",
      answerId: 56
    },
    {
      "id": 56,
      "a_id": 205,
      "user": "用戶2",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303713",
      answerId:''
    },
    {
      "id": 57,
      "a_id": 205,
      "user": "用戶2",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303713",
      answerId:''
    },
  ]
 var result = [{
    "id": 52,
    "a_id": 205,
    "user": "用戶3",
    "website": "",
    "msg": "王菲菲",
    "createTime": "1533303716",
    "answerId": "",
    "answerArr": [{
      "id": 54,
      "a_id": 205,
      "user": "用戶5",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303716",
      "answerId": 52
    }, 
      {"id": 55, "a_id": 205, "user": "用戶5", "website": "", "msg": "王菲菲", "createTime": "1533303716", "answerId": 52}
      ]
  }, {
    "id": 56,
    "a_id": 205,
    "user": "用戶2",
    "website": "",
    "msg": "王菲菲",
    "createTime": "1533303713",
    "answerId": "",
    "answerArr": [{
      "id": 51,
      "a_id": 205,
      "user": "用戶2",
      "website": "",
      "msg": "王菲菲",
      "createTime": "1533303713",
      "answerId": 56
    }]
  }, {"id": 57, "a_id": 205, "user": "用戶2", "website": "", "msg": "王菲菲", "createTime": "1533303713", "answerId": ""}]

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

相關(guān)代碼

// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

回答
編輯回答
萌面人

推薦array-to-tree npm 包

var arrayToTree = require('array-to-tree');
arrayToTree(COMMENT_MOCK_DATA_ORIGIN, {
  parentProperty: 'answerId'
})
2018年9月2日 03:05
編輯回答
怪痞

這是我寫的看看有什么缺點(diǎn)可以改進(jìn)的?

const getMyCommenData=(COMMENT_MOCK_DATA_ORIGIN)=> {
  let mapping = {answerId: []}
  let grande_one = COMMENT_MOCK_DATA_ORIGIN.filter(v => {
    let {answerId = ''} = v;
    if (answerId) {
      mapping[answerId] = []
    }
    return answerId === '' || answerId==null||answerId===0;
  })
  COMMENT_MOCK_DATA_ORIGIN.forEach(v => {
    let {answerId = ''} = v;
    if (answerId in mapping) {
      mapping[answerId].push(v)
    }
  })
  return grande_one.map(v => {
    let {id} = v;
    if (id in mapping) {
      v.answerArr = mapping[id]
    }
    return v;
  });
}
2017年11月30日 07:15