如何将 ID 数组与包含其 ID 作为属性名称一部分的对象属性进行匹配
how to match array of ids to object properties that contains their id as part of the property name
我有一个对象,该对象在属性名称中添加了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>');
相关文章:
- 使用Google'设置ID属性;s映射API
- 如何排除对象'在$.toJSON之前或期间的s id属性
- 为什么可以't我得到特定跨度的ID属性
- 如何传递id属性而不是值
- 设置 HTML 自定义属性与在 ID 属性中存储信息
- 如何选择并单击没有 id 属性的按钮
- 在标头中显示id属性
- 如何使用json对象作为li元素的id属性或使用javascript锚定标记id
- 如何将javascript变量值设置为html元素的id属性
- 根据单选按钮id属性调用ajax jquery
- 如何用angular js对HTML元素的id属性进行条件设置
- 在模板angularJs中从控制器填充id属性的值
- JavaScript文档.getElementById(“id”)和元素id属性
- 如何查找所有具有匹配类名的id属性
- 使用 jQuery 通过 ID 属性选择 HTML 元素
- 从 Zend 窗体元素中删除 id 属性
- 使用 for 循环,如何迭代对象以向其添加 id 属性
- 查找 DOM 上具有“id”属性的所有元素
- 页面上第二个表单所需的唯一 id 属性
- 在包含许多元素的页面中首选 id 属性或数据属性