如何将 ID 数组与包含其 ID 作为属性名称一部分的对象属性进行匹配

how to match array of ids to object properties that contains their id as part of the property name

本文关键字:ID 属性 对象 一部分 数组 包含其      更新时间:2023-09-26

我有一个对象,该对象在属性名称中添加了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都很好。 这是工作 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,
 }

这应该可以做到:

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

这是我

使用 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;

我还更新了你的弹奏。

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

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

如果您对这个算法还有其他疑问,请随时提出,但不要指望我能为你实现整个算法;)

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

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