跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 这题如何实现比较好
未分類
31 5 月 2020

这题如何实现比较好

这题如何实现比较好

資深大佬 : pyplayer 1

把 id 一样的合并

var array = [   { id: 1, spec: 1 },   { id: 5, spec: 2 },   { id: 2, spec: 5 },   { id: 2, spec: 10 },   { id: 1, spec: 12 },   { id: 3, spec: 12 },   { id: 4, spec: 15 },   { id: 3, spec: 16 } ] 转变为 [   { id: 1, spec: [ 1, 12 ] },   { id: 5, spec: [ 2 ] },   { id: 2, spec: [ 5, 10 ] },   { id: 3, spec: [ 12, 16 ] },   { id: 4, spec: [ 15 ] } ]  

自己的思路似乎很烦 大概思路是出现过得 id 添加到了 idarray 里,然后迭代数组每一项,id 出现过就创建一个新对象,没有就选出那个对象往数组后面添加东西

function arrayChange(array){   let idArray = []   let newArray = []   for (let index = 0; index < array.length; index++) {     const element = array[index];     if (idArray.includes(element.id)) {       let curEle = newArray.filter(item => item.id === element.id)       curEle[0].spec.push(element.spec)     } else {       idArray.push(element.id)       let obj = {}       obj.id = element.id       obj.spec = []       obj.spec.push(element.spec)       newArray.push(obj)     }   }   return newArray  } console.log(arrayChange(array)) 

大佬有話說 (10)

  • 資深大佬 : ynohoahc

    丑丑地实现一下.
    array.reduce((total, prev) => {
    let item = total.find(item => item.id === prev.id)
    if(!item) {
    let ret = {
    id: prev.id,
    spec: [prev.spec]
    }
    total.push(ret)
    } else {
    item.spec.push(prev.spec)
    }
    return total
    }, [])

  • 資深大佬 : jmc891205

    额 js 没有 hashmap 一类的东西吗

  • 資深大佬 : jmc891205

    @jmc891205 每次插入都要遍历那太慢了

  • 資深大佬 : rabbbit

    function solution(arr) {
      const table =new Map();
      for (const {id, spec} of arr) {
       if (!table.has(id)) table.set(id, []);
       table.get(id).push(spec);
     }
      const result = [];
      for (const [id, spec] of table.entries()) {
       result.push({id, spec})
     }
      return result;
    }

  • 主 資深大佬 : pyplayer

    @ynohoahc 我该去复习下 reduce 了。。。

  • 資深大佬 : rabbbit

    function solution(arr) {
      const table =new Map();
      for (const {id, spec} of arr) {
       if (!table.has(id)) table.set(id, []);
       table.get(id).push(spec);
     }
      return […table].map(([id, spec]) => ({id, spec}));
    }

  • 資深大佬 : haha370104

    Object.entries(
    array.reduce((previousValue, currentValue) => {
    previousValue[currentValue.id] = (
    previousValue[currentValue.id] || []
    ).concat(currentValue.spec)
    return previousValue
    }, {})
    ).map(([id, spec]) => ({ id, spec }))

    这应该是我能想出来最短的写法了……

  • 主 資深大佬 : pyplayer

    @rabbbit 非常感谢 我去做个笔记

  • 資深大佬 : linZ

    用个 Map 不好么。。。rabit 的写法就好了

  • 資深大佬 : loonghk

    // 手痒,为了回答这个,特地注册了个账号

    function arrayChange(arr){

    var res = {};

    arr.forEach(d=>{ if(!res[d.id]){ res[d.id]=[]; } res[d.id].push(d.spec) }); // res = {1:[1,12],2:[5,10],3:[12,16],4:[15],5:[2]}

    return Object.keys(res).map(k=>({‘id’:k,’spec’:res[k]}));

    }

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具