js--Merge other items in the two-dimensional array object according to the same attribute value

let arr = [
      { id: 1, list: { id: 66 } },
      { id: 1, list: { id: 44 } },
      { id: 2, list: { id: 55 } },
      { id: 2, list: { id: 33 } },
      { id: 3, list: { id: 11 } },
      { id: 3, list: { id: 22 } }
    ]

    let tempArr = [], newArr = []
    for (let i = 0; i < arr.length; i++) {
      if (tempArr.indexOf(arr[i].id) === -1) {
        newArr.push({
          id: arr[i].id,
          list: [arr[i].list]
        })
        tempArr.push(arr[i].id);
      } else {
        for (let j = 0; j < newArr.length; j++) {
          if (newArr[j].id == arr[i].id) {
            newArr[j].list.push(arr[i].list)
          }
        }
      }
    }

    console.log(newArr)

If it is useful to you, pay attention to the blogger's applet, log in to give support, and any open source and useful source code will be uploaded to the applet in the future

 

Guess you like

Origin blog.csdn.net/qq_42543264/article/details/121142196