根据键值对过滤对象

时间:2019-04-16 09:34:28

标签: javascript json object

我有一个JSON对象

  {
      "value": "0",
      "minute": "2019-04-16T01:48",
      "action": "verb",
      "label": "noun",
      "id": "appid1"
  },
  {
      "value": "7",
      "minute": "2019-04-16T01:49",
      "action": "verb",
      "label": "noun",
      "id": "appid1"
  },
  {
      "value": "18",
      "minute": "2019-04-16T01:50",
      "action": "verb",
      "label": "noun",
      "id": "appid1"
  },
  {
      "value": "0",
      "minute": "2019-04-16T01:31",
      "action": "verb",
      "label": "noun",
      "id": "appid2"
  },
  {
      "value": "19",
      "minute": "2019-04-16T01:51",
      "action": "verb",
      "label": "noun",
      "id": "appid2"
  },
  {
      "value": "19",
      "minute": "2019-04-16T01:52",
      "action": "verb",
      "label": "noun",
      "id": "appid2"
  },
  {
      "value": "19",
      "minute": "2019-04-16T01:53",
      "action": "verb",
      "label": "noun",
      "id": "appid1"
  },
  {
      "value": "18",
      "minute": "2019-04-16T01:54",
      "action": "verb",
      "label": "noun",
      "id": "appid1"
  },
  {
      "value": "17",
      "minute": "2019-04-16T01:55",
      "action": "verb",
      "label": "noun",
      "id": "appid1"
  }

我想要创建三个存储桶(A,B,C)。存储桶A具有前三个appid1对象,而存储桶B具有appid2对象,而存储桶C具有appid1对象的第三组也是最后一组。

我尝试做类似这样的事情

let startIndex = 0;
let endIndex = -1;

let currentAppID = "appid1"
        data.forEach((d, index)=> {
            if(d["label"] === 'noun' && d["id"]=== currentGameId && d["action"]==="verb") {
                startIndex = index
            }

            if(d["label"] === 'noun' && d["id"]!== currentGameId && d["event.action"]==="verb") {
                endIndex = index
            }
        })

 let bucketItems = data.slice(startIndex,endIndex)

但是这段代码完全给了我荒谬的结果。您能告诉我我在做什么错吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您可以缩小数组并检查last对象和实际对象o是否具有相同的id,否则添加一个新组。

var data = [{ value: "0", minute: "2019-04-16T01:48", action: "verb", label: "noun", id: "appid1" }, { value: "7", minute: "2019-04-16T01:49", action: "verb", label: "noun", id: "appid1" }, { value: "18", minute: "2019-04-16T01:50", action: "verb", label: "noun", id: "appid1" }, { value: "0", minute: "2019-04-16T01:31", action: "verb", label: "noun", id: "appid2" }, { value: "19", minute: "2019-04-16T01:51", action: "verb", label: "noun", id: "appid2" }, { value: "19", minute: "2019-04-16T01:52", action: "verb", label: "noun", id: "appid2" }, { value: "19", minute: "2019-04-16T01:53", action: "verb", label: "noun", id: "appid1" }, { value: "18", minute: "2019-04-16T01:54", action: "verb", label: "noun", id: "appid1" }, { value: "17", minute: "2019-04-16T01:55", action: "verb", label: "noun", id: "appid1" }],
    result = data.reduce((r, o, i, { [i - 1]: last }) => {
        if (!last || o.id !== last.id) r.push([]);
        r[r.length - 1].push(o);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }