JS 数组要根据里面的值分成若干个小对象

WytheChan
  • 766

要处理的数组结构是这样的

 var imgs = [
        {
          "id": 4,
          "text": "0102051911160Y04",
          "addtime": "2019-11-20"
        },
        {
          "id": 5,
          "text": "0102051911160Y04",
          "addtime": "2019-11-20"
        },
        {
          "id": 6,
          "text": "0102051911160Y04",
          "addtime": "2019-11-20"
        },
        {
          "id": 4,
          "text": "0102051911160Y04",
          "addtime": "2019-11-21"
        },
        {
          "id": 5,
          "text": "0102051911160Y04",
          "addtime": "2019-11-21"
        },
        {
          "id": 6,
          "text": "0102051911160Y04",
          "addtime": "2019-11-21"
        },
        {
          "id": 4,
          "text": "0102051911160Y04",
          "addtime": "2019-11-23"
        },
        {
          "id": 5,
          "text": "0102051911160Y04",
          "addtime": "2019-11-23"
        },
        {
          "id": 6,
          "text": "0102051911160Y04",
          "addtime": "2019-11-23"
        },

      ]

我想要的结果是这样的:

var obj = {
  "addtime": "2019-11-20",
  "id": 4,
  "text":[
    {
      "id": 4,
      "text": "0102051911160Y04",
      "addtime": "2019-11-20",
    },
    {
      "id": 4,
      "text": "0102051911160Y04",
      "addtime": "2019-11-20",
    },
  ]
}

就是我想要把相同的时间和ID分类成一个对象,然后对象里的text是一个数组。
现在的难点就在于时间和id都是不固定的,就是没有固定的值判断,要自己循环找出相同的数组和时间,大家有什么简单点的办法吗?

回复
阅读 925
2 个回答
zangeci
  • 10.6k
✓ 已被采纳
var res = Object.values(imgs.reduce((res, v) => {
  const {addtime, id} = v;
  const key = addtime + '_' + id;
  const obj = res[key];
  if(obj) {
    obj.text.push(v);
  } else {
    res[key] = {
      id,
      addtime,
      text: [v],
    }
  }
  return res;
}, {}));
_AN_
  • 343

var imgs = [

    {
      "id": 4,
      "text": "0102051911160Y04",
      "addtime": "2019-11-20"
    },
    {
      "id": 5,
      "text": "0102051911160Y04",
      "addtime": "2019-11-20"
    },
    {
      "id": 6,
      "text": "0102051911160Y04",
      "addtime": "2019-11-20"
    },
    {
      "id": 4,
      "text": "0102051911160Y04",
      "addtime": "2019-11-21"
    },
    {
      "id": 5,
      "text": "0102051911160Y04",
      "addtime": "2019-11-21"
    },
    {
      "id": 6,
      "text": "0102051911160Y04",
      "addtime": "2019-11-21"
    },
    {
      "id": 4,
      "text": "0102051911160Y04",
      "addtime": "2019-11-23"
    },
    {
      "id": 5,
      "text": "0102051911160Y04",
      "addtime": "2019-11-23"
    },
    {
      "id": 6,
      "text": "0102051911160Y04",
      "addtime": "2019-11-23"
    },

  ]

var oImgs = new Object()

for(let i = 0;i<imgs.length;i++){

if(imgs[i].id in oImgs){
    oImgs[imgs[i].id].push({
        id:imgs[i].text,
        addtime:imgs[i].addtime,
        text:imgs[i].text
    })
}else{
    oImgs[imgs[i].id] = []
}

}

result = []

for(id in oImgs){

result.push({
    id:id,
    text: oImgs[id]
})

}

console.log(result)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏