处理这个对象数组并将其组合成一个

时间:2016-06-26 08:48:03

标签: javascript

我有这个对象。

Started GET "/users" for 127.0.0.1 at 2016-06-26 03:45:39 -0500
Processing by UsersController#index as HTML
  User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1547], ["LIMIT", 1]]
  Role Load (1.3ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1547]]
Redirected to http://localhost:3000/
Completed 302 Found in 19ms (ActiveRecord: 2.4ms)


Started GET "/users" for 127.0.0.1 at 2016-06-26 03:45:39 -0500
Processing by UsersController#index as HTML
  User Load (1.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1547], ["LIMIT", 1]]
  Role Load (2.8ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1547]]
Redirected to http://localhost:3000/
Completed 302 Found in 24ms (ActiveRecord: 4.0ms)

我想通过将一些相关对象分组为一个对象来减少对象的数量。

生成的数组应该如下所示;

var orig_array =   [
    { "MSG_ID": { "lgth": "1", "payload": 0 } },
    { "STATE_1": { "lgth": "1", "payload": "XX" } },
    { "STATE_2": { "lgth": "1", "payload": "XXX" } },
    { "MSG_ID": { "lgth": "1", "payload": 1 } },
    { "STATE_1": { "lgth": "1", "payload": "X" } },
    { "STATE_2": { "lgth": "1", "payload": "XX" } },
    { "STATE_3": { "lgth": "6", "payload": "XXX" } },
    { "MSG_ID": { "lgth": "1", "payload": 2 } },
    { "STATE_1": { "lgth": "1", "payload": "XXXX" } }
];

var processed_array = [ { "MSG_ID": { "lgth": "1", "payload": 0 }, "STATE_1": { "lgth": "1", "payload": "XX" },"STATE_2": { "lgth": "1", "payload": "XXX" } }, { "MSG_ID": { "lgth": "1", "payload": 1 },"STATE_1": { "lgth": "1", "payload": "X" },"STATE_2": { "lgth": "1", "payload": "XX" },"STATE_3": { "lgth": "6", "payload": "XXX" } }, { "MSG_ID": { "lgth": "1", "payload": 2 },"STATE_1": { "lgth": "1", "payload": "XXXX" } } ]; 中,有3个orig_array对象。随后的"MSG_ID"个对象将合并到上面的"STATE_X"对象中。我已经盯着这个问题一段时间了。任何人都可以提供帮助或提供一些提示作为先行者?

4 个答案:

答案 0 :(得分:6)

您可以使用Array#reduce并查找'MSG_ID'作为群组更改。



var array = [{ "MSG_ID": { "lgth": "1", "payload": 0 } }, { "STATE_1": { "lgth": "1", "payload": "XX" } }, { "STATE_2": { "lgth": "1", "payload": "XXX" } }, { "MSG_ID": { "lgth": "1", "payload": 1 } }, { "STATE_1": { "lgth": "1", "payload": "X" } }, { "STATE_2": { "lgth": "1", "payload": "XX" } }, { "STATE_3": { "lgth": "6", "payload": "XXX" } }, { "MSG_ID": { "lgth": "1", "payload": 2 } }, { "STATE_1": { "lgth": "1", "payload": "XXXX" } }],
    grouped = array.reduce(function (r, a) {
        var key = Object.keys(a)[0];
        if (key === 'MSG_ID') {
            r.push({});
        }
        r[r.length - 1][key] = a[key];
        return r;
    }, []);

console.log(grouped);




答案 1 :(得分:2)

我认为这将是正确答案



var orig_json = [{
  "MSG_ID": { "lgth": "1", "payload": 0 }
}, {
  "STATE_1": { "lgth": "1", "payload": "XX" }
}, {
  "STATE_2": { "lgth": "1", "payload": "XXX" }
}, {
  "MSG_ID": { "lgth": "1", "payload": 1 }
}, {
  "STATE_1": { "lgth": "1", "payload": "X" }
}, {
  "STATE_2": { "lgth": "1", "payload": "XX" }
}, {
  "STATE_3": { "lgth": "6", "payload": "XXX" }
}, {
  "MSG_ID": { "lgth": "1", "payload": 2 }
}, {
  "STATE_1": { "lgth": "1", "payload": "XXXX" }
}];

var out = [],
  group = {};

orig_json
  .reduce((p, c) => p.concat(
    Object.keys(c).map(k => ({
      key: k,
      value: c[k]
    }))
  ), []).forEach(v => {
    if (v.key === "MSG_ID") {
      group = {};
      out.push(group);
    }
    group[v.key] = v.value
  });
console.log(out)




答案 2 :(得分:2)

你可以使用许多js函数eg.sort,map,forEach ...等。我尝试使用我的首选函数.sort

    i = -1;
    processed_array = [];
    orig_array.sort(function (r) {
        var k = Object.keys(r)[0];        
        if(k === "MSG_ID"){          
          i++;
          processed_array.push({});
        }
        processed_array[i][k] = r[k];
        return processed_array;
    }); 
console.log(processed_array);

答案 3 :(得分:1)

我认为你需要一个groupBy方法:

Array.prototype.groupBy = function(expresion) {
  var collection = [];
  this.forEach((v, i, a) => {
    var key = expresion(v, i, a);
    var group = collection.find(g => g.key === key);
    if (!group) {
      group = [];
      group.key = key;
      collection.push(group);
    }
    group.push(v);
  });

  return collection;
};

var orig_json = [{
  "MSG_ID": { "lgth": "1", "payload": 0 }
}, {
  "STATE_1": { "lgth": "1", "payload": "XX" }
}, {
  "STATE_2": { "lgth": "1", "payload": "XXX" }
}, {
  "MSG_ID": { "lgth": "1", "payload": 1 }
}, {
  "STATE_1": { "lgth": "1", "payload": "X" }
}, {
  "STATE_2": { "lgth": "1", "payload": "XX" }
}, {
  "STATE_3": { "lgth": "6", "payload": "XXX" }
}, {
  "MSG_ID": { "lgth": "1", "payload": 2 }
}, {
  "STATE_1": { "lgth": "1", "payload": "XXXX" }
}];

var newObj =orig_json
  .reduce((p, c) => p.concat(Object.keys(c).map(k => c[k])), [])
  .groupBy(v => Object.keys(v)[0]);
console.log(newObj);