Javascript 数组重建 merging/filtering
Javascript array reconstruct with merging/filtering
我有两个 javascript 阵列一个
var sectors = [
{ name: 'Fishing' }, {name: 'Bowling'}...
];
另一个
var organizations = [
{name: 'Carson ind', tel: '1545454', fax: 1215454, sectors: ['Banking', 'Fishing']},
{name: 'Superman & Co.', tel: '1545454', fax: 1215454, sectors: ['Financing', 'Banking']},
....
];
我如何用这两个具有以下结构的数组创建新数组。
var newArray = [
{sector : 'Banking', organizations :
[
{name: 'Superman & Co.', tel: '1545454', fax: 1215454, sectors: ['Financing', 'Banking']},
....
]
}
];
现在我正在使用下划线和我的代码
var newArray = [];
_.each(vm.allSectors, function(sec){
newArray[sec.name] = _.filter(vm.allOrganizations, function(org) {
return _.filter(org.sector, function(sector){
return sector === sec.name;
});
});
});
它没有给我我想要的结果,它只是 return 很多扇区数组。
基本上是 user6188402 的回答,但有固定的 lambdas
result = sectors.map(s => {
return {
sector: s.name,
organizations: organizations.filter(o => o.sectors.indexOf(s.name) > -1)
}
});
此提案使用一个对象作为所需部门并迭代 organisations
并创建一个新对象并将实际数据插入组织 属性。
var sectors = [{ name: 'Fishing' }, { name: 'Bowling' }],
sectorHash = {},
organizations = [{ name: 'Carson ind', tel: '1545454', fax: 1215454, sectors: ['Banking', 'Fishing'] }, { name: 'Superman & Co.', tel: '1545454', fax: 1215454, sectors: ['Financing', 'Banking'] }],
newArray = [];
sectors.forEach(function (a) {
sectorHash[a.name] = true;
});
organizations.forEach(function (a) {
a.sectors.forEach(function (b) {
if (sectorHash[b]) {
if (!this[b]) {
this[b] = { sector: b, organzations: [] };
newArray.push(this[b]);
}
this[b].organzations.push(a);
}
}, this);
}, Object.create(null));
document.write('<pre>' + JSON.stringify(newArray, 0, 4) + '</pre>');
我有两个 javascript 阵列一个
var sectors = [
{ name: 'Fishing' }, {name: 'Bowling'}...
];
另一个
var organizations = [
{name: 'Carson ind', tel: '1545454', fax: 1215454, sectors: ['Banking', 'Fishing']},
{name: 'Superman & Co.', tel: '1545454', fax: 1215454, sectors: ['Financing', 'Banking']},
....
];
我如何用这两个具有以下结构的数组创建新数组。
var newArray = [
{sector : 'Banking', organizations :
[
{name: 'Superman & Co.', tel: '1545454', fax: 1215454, sectors: ['Financing', 'Banking']},
....
]
}
];
现在我正在使用下划线和我的代码
var newArray = [];
_.each(vm.allSectors, function(sec){
newArray[sec.name] = _.filter(vm.allOrganizations, function(org) {
return _.filter(org.sector, function(sector){
return sector === sec.name;
});
});
});
它没有给我我想要的结果,它只是 return 很多扇区数组。
基本上是 user6188402 的回答,但有固定的 lambdas
result = sectors.map(s => {
return {
sector: s.name,
organizations: organizations.filter(o => o.sectors.indexOf(s.name) > -1)
}
});
此提案使用一个对象作为所需部门并迭代 organisations
并创建一个新对象并将实际数据插入组织 属性。
var sectors = [{ name: 'Fishing' }, { name: 'Bowling' }],
sectorHash = {},
organizations = [{ name: 'Carson ind', tel: '1545454', fax: 1215454, sectors: ['Banking', 'Fishing'] }, { name: 'Superman & Co.', tel: '1545454', fax: 1215454, sectors: ['Financing', 'Banking'] }],
newArray = [];
sectors.forEach(function (a) {
sectorHash[a.name] = true;
});
organizations.forEach(function (a) {
a.sectors.forEach(function (b) {
if (sectorHash[b]) {
if (!this[b]) {
this[b] = { sector: b, organzations: [] };
newArray.push(this[b]);
}
this[b].organzations.push(a);
}
}, this);
}, Object.create(null));
document.write('<pre>' + JSON.stringify(newArray, 0, 4) + '</pre>');