0

我有一个在属性名称中添加了 section_id 的对象。

 "price_min-3155": 54,
 "price_min-12863": 23,
 "price_min-16152": 43, etc...

我有一个 section_ids 数组

var sectionIdArray = [3155,12863,16152];

同样在对象中,我有属性 subdivision_id 需要保持原样。

  "subdivision_id": 3500,

我正在寻找的最终结果是按 section_id 对属性进行分组,还包括 subdivision_id。需要有看起来像这样的对象数组。

newArry = [{
 subdivision_id: 3500,
 section_id: 3155, //"section_id-3155": 3155,
 price_min:54, //"price_min-3155": 54,
 price_max: 34, // "price_max-3155": 34,
 units_occ: 54, //"units_occ-3155": 54,
 etc...
},{
 subdivision_id: 3500,
 section_id: 12863, //"section_id-12863": 12863,
 price_min:23, //"price_min-12863": 23,
 price_max: 56, //  "price_max-12863": 56,
 units_occ: 9, //"units_occ-12863": 9,
 etc...
}]

javascript、jquery、lodash 和 linq.js 都很好。这是工作的笨蛋

工作的笨蛋

  $scope.model = {
"section_id-3155": 3155,
"price_min-3155": 54,
"units_total-3155": 323,
"price_max-3155": 34,
"units_occ-3155": 54,
"inv_mod-3155": 5,
"inv_fin-3155": 6,
"inv_vdl-3155": 35,
"inv_uc-3155": 45,
 }
4

4 回答 4

2

这应该这样做:

var data = {
  "section_id-3155": 3155,
  "price_min-3155": 54,
  "units_total-3155": 323,
  "price_max-3155": 34,
  "units_occ-3155": 54,
  "inv_mod-3155": 5,
  "inv_fin-3155": 6,
  "inv_vdl-3155": 35,
  "inv_uc-3155": 45,
  "inv_fut-3155": 45,
  "inv_con-3155": 45,
  "fs_excav-3155": true,
  "fs_streets-3155": true,
  "fs_stakes-3155": true,
  "section_id-12863": 12863,
  "subdivision_id": 3500,
  "price_min-12863": 23,
  "price_max-12863": 56,
  "units_occ-12863": 9,
  "inv_mod-12863": 32,
  "inv_fin-12863": 56,
  "inv_vdl-12863": 123,
  "inv_uc-12863": 54,
  "inv_fut-12863": 76,
  "inv_con-12863": 23,
  "units_total-12863": 87,
  "$$hashKey-12863": "object:60",
  "section_id-16152": 16152,
  "price_min-16152": 43,
  "units_total-16152": 994,
  "price_max-16152": 9,
  "units_occ-16152": 65,
  "inv_mod-16152": 765,
  "inv_fin-16152": 34,
  "inv_vdl-16152": 65,
  "inv_uc-16152": 6,
  "inv_fut-16152": 7,
  "fs_excav-12863": true,
  "fs_paved-12863": true,
  "fs_equip-12863": true,
  "fs_stakes-12863": true,
  "fs_equip-16152": true,
  "fs_excav-16152": true,
  "fs_paved-16152": true,
  "fs_streets-16152": true
};

var sectionIdArray = [3155, 12863, 16152];

var objectArray = sectionIdArray.map(function(id) {
  var res = {
    subdivision_id: data.subdivision_id
  };
  Object.keys(data).forEach(function(key) {
    var regex = new RegExp("-" + id + "$");
    if (regex.test(key)) {
      res[key.replace(regex, "")] = data[key];
    }
  });
  return res;
});

//output
document.body.innerHTML = JSON.stringify(objectArray);

于 2016-01-13T21:37:05.760 回答
1

我会给你一个可能的抽象算法的想法:

  • 遍历 $scope.model 中的所有键 k (for var key in $scope.model) { ...}
  • 在每一步中迭代 sectionIdArray 中的每个值 v
  • 如果 v 是字符串 k (k.indexOf(v) !== -1) 的一部分,则将其放入这样的“篮子”(例如与此 sectionId 相关的数组)
  • 通过评估那里的数据(获取最小值/最大值等)并从原始对象复制 subvision_id,为每个篮子创建一个新的结果数组条目

如果您对此算法还有任何疑问,请不要犹豫,但不要指望我为您实现整个算法;)

于 2016-01-13T21:30:21.643 回答
1

您可以使用临时对象,然后制作所需的数组。

var data = { "section_id-3155": 3155, "price_min-3155": 54, "units_total-3155": 323, "price_max-3155": 34, "units_occ-3155": 54, "inv_mod-3155": 5, "inv_fin-3155": 6, "inv_vdl-3155": 35, "inv_uc-3155": 45, "inv_fut-3155": 45, "inv_con-3155": 45, "fs_excav-3155": true, "fs_streets-3155": true, "fs_stakes-3155": true, "section_id-12863": 12863, "subdivision_id": 3500, "price_min-12863": 23, "price_max-12863": 56, "units_occ-12863": 9, "inv_mod-12863": 32, "inv_fin-12863": 56, "inv_vdl-12863": 123, "inv_uc-12863": 54, "inv_fut-12863": 76, "inv_con-12863": 23, "units_total-12863": 87, "section_id-16152": 16152, "price_min-16152": 43, "units_total-16152": 994, "price_max-16152": 9, "units_occ-16152": 65, "inv_mod-16152": 765, "inv_fin-16152": 34, "inv_vdl-16152": 65, "inv_uc-16152": 6, "inv_fut-16152": 7, "fs_excav-12863": true, "fs_paved-12863": true, "fs_equip-12863": true, "fs_stakes-12863": true, "fs_equip-16152": true, "fs_excav-16152": true, "fs_paved-16152": true, "fs_streets-16152": true },
    keys = Object.keys(data),
    result = keys.reduce(function (r, k) {
        var p = k.split('-'), o;
        if (p[1]) {
            if (!(p[1] in r.temp)) {
                o = { subdivision_id: data['subdivision_id'] };
                r.a.push(o);
                r.temp[p[1]] = o;
            }
            r.temp[p[1]][p[0]] = data[k];
        }
        return r;
    }, { temp: {}, a: [] }).a;

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

于 2016-01-13T21:42:23.550 回答
1

这是我使用lodash. 这适用于您的示例数据,但可以修改以符合更严格的规则。这只是一个如何使用lodash.

  $scope.model = {
    "section_id-3155": 3155,
    "price_min-3155": 54,
    "units_total-3155": 323,
    "price_max-3155": 34,
    "units_occ-3155": 54,
    "inv_mod-3155": 5,
    "inv_fin-3155": 6,
    "inv_vdl-3155": 35,
    "inv_uc-3155": 45,
    "inv_fut-3155": 45,
    "inv_con-3155": 45,
    "fs_excav-3155": true,
    "fs_streets-3155": true,
    "fs_stakes-3155": true,
    "section_id-12863": 12863,
    "subdivision_id": 3500,
    "price_min-12863": 23,
    "price_max-12863": 56,
    "units_occ-12863": 9,
    "inv_mod-12863": 32,
    "inv_fin-12863": 56,
    "inv_vdl-12863": 123,
    "inv_uc-12863": 54,
    "inv_fut-12863": 76,
    "inv_con-12863": 23,
    "units_total-12863": 87,
    "$$hashKey-12863": "object:60",
   "section_id-16152": 16152,
    "price_min-16152": 43,
    "units_total-16152": 994,
    "price_max-16152": 9,
    "units_occ-16152": 65,
    "inv_mod-16152": 765,
    "inv_fin-16152": 34,
    "inv_vdl-16152": 65,
    "inv_uc-16152": 6,
    "inv_fut-16152": 7,
    "fs_excav-12863": true,
    "fs_paved-12863": true,
    "fs_equip-12863": true,
    "fs_stakes-12863": true,
    "fs_equip-16152": true,
    "fs_excav-16152": true,
    "fs_paved-16152": true,
    "fs_streets-16152": true
};

  $scope.res = {};

  var res = _($scope.model)
    .pairs()
    .groupBy(function (val) {
      var parts = val[0].split('-');
      return parts[parts.length-1];
    })
    .transform(function (result, val, key, src) {
      if (!isNaN(key)) { // is number
        key = +key;
        result[key] = _(val)
          .zipObject()
          .transform(function (result, val, key) {
            var parts = key.split('-'), newKey;
            parts.splice(-1, 1);
            newKey = parts.join('-');
            result[newKey] = val;
          }, {})
          .value();
        result[key]['subdivision_id'] = src['subdivision_id'][0][1];
      }
    }, {})
    .value();

  $scope.res = res;

我还更新了你的plunker

于 2016-01-13T22:04:42.500 回答