【问题标题】:how to match array of ids to object properties that contains their id as part of the property name如何将 id 数组与包含其 id 作为属性名称一部分的对象属性匹配
【发布时间】:2016-01-13 21:05:20
【问题描述】:

我有一个在属性名称中添加了 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 都很好。这是工作的笨蛋

working plunker

  $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,
 }

【问题讨论】:

  • 那么,如果你有一个工作的 plunker,问题是什么?
  • 对不起,我的意思是装数据的人

标签: javascript jquery angularjs lodash linq.js


【解决方案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,
  "$$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);

【讨论】:

    【解决方案2】:

    这是我使用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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 不想让每个人都完成我的工作。只是其中一些我不太了解的东西。但是知道从什么开始是很棒的。谢谢你的帖子! :-)
      【解决方案4】:

      你可以使用一个临时对象,然后创建想要的数组。

      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>');

      【讨论】:

        猜你喜欢
        • 2018-05-07
        • 2011-02-17
        • 2022-09-30
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 2015-08-27
        相关资源
        最近更新 更多