On a problem of js data structure Transformation

there is a comment scenario where id is unique, answerId and id correspond, and answerId is the person who replied to id. You need to convert COMMENT_MOCK_DATA_ORIGIN data into result data structure, which is equivalent to an additional field answerArr into a reply array.

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": ""}]

the environmental background of the problems and what methods you have tried

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Apr.03,2021

recommend array-to-tree npm package

var arrayToTree = require('array-to-tree');
arrayToTree(COMMENT_MOCK_DATA_ORIGIN, {
  parentProperty: 'answerId'
})

I wrote this to see if there are any shortcomings that can be improved.

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;
  });
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35ef8-41014.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35ef8-41014.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?