给出 this JSON结果,或者如下所示:
{
"facetItems": [{
"Company_Name": "Sample Brookline Company",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Brookline",
"State": "MA",
"Latitude": 42.3325651,
"Longitude": -71.1140136,
"Company_Type": [{
"ID": "id2",
"Display_Value": "A"
}, {
"ID": "id4",
"Display_Value": "B"
}]
}, {
"Company_Name": "Sample Everett Company",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Everett",
"State": "MA",
"Latitude": 42.4030189,
"Longitude": -71.0454874,
"Company_Type": [{
"ID": "id1",
"Display_Value": "G"
}, {
"ID": "id2",
"Display_Value": "A"
}, {
"ID": "id3",
"Display_Value": "E"
}, {
"ID": "id4",
"Display_Value": "B"
}, {
"ID": "id5",
"Display_Value": "C"
}]
}, {
"Company_Name": "Sample Wakefield Company 1",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091051,
"Longitude": -71.0675369,
"Company_Type": [{
"ID": "id1",
"Display_Value": "G"
}, {
"ID": "id2",
"Display_Value": "A"
}, {
"ID": "id3",
"Display_Value": "E"
}, {
"ID": "id4",
"Display_Value": "B"
}, {
"ID": "id5",
"Display_Value": "C"
}]
}, {
"Company_Name": "Sample Wakefield Company 2",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091059,
"Longitude": -71.0675379,
"Company_Type": [{
"ID": "id1",
"Display_Value": "G"
}, {
"ID": "id2",
"Display_Value": "A"
}, {
"ID": "id3",
"Display_Value": "E"
}, {
"ID": "id4",
"Display_Value": "B"
}]
}, {
"Company_Name": "Sample Wakefield Company 3",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091089,
"Longitude": -71.0675399,
"Company_Type": [{
"ID": "id3",
"Display_Value": "E"
}, {
"ID": "id5",
"Display_Value": "C"
}]
}, {
"Company_Name": "Sample Wakefield Company 4",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091101,
"Longitude": -71.0675401,
"Company_Type": [{
"ID": "id1",
"Display_Value": "G"
}, {
"ID": "id2",
"Display_Value": "A"
}, {
"ID": "id3",
"Display_Value": "E"
}]
}, {
"Company_Name": "Sample Wakefield Company 5",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091131,
"Longitude": -71.0675431,
"Company_Type": [{
"ID": "id3",
"Display_Value": "E"
}, {
"ID": "id4",
"Display_Value": "B"
}, {
"ID": "id5",
"Display_Value": "C"
}]
}, {
"Company_Name": "Sample Wakefield Company 6",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091151,
"Longitude": -71.0675451,
"Company_Type": [{
"ID": "id1",
"Display_Value": "G"
}, {
"ID": "id2",
"Display_Value": "A"
}, {
"ID": "id4",
"Display_Value": "B"
}, {
"ID": "id5",
"Display_Value": "C"
}]
}]
}

我想通过以下内容管理系统返回的对象集合facetItems
对ID
进行分组,因为此列表决定了顺序:
"Company_Type": [
{ "ID": "id1", "Display_Value": "G" },
{ "ID": "id2", "Display_Value": "A" },
{ "ID": "id3", "Display_Value": "E" },
{ "ID": "id4", "Display_Value": "B" },
{ "ID": "id5", "Display_Value": "C" }
]
结果应如何
{
"Company_Type": [
{
"ID": "id1",
"Display_Value": "G",
"Companies": [
{
"Hospital_Name": "Sample Brookline Company",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Brookline",
"State": "MA",
"Latitude": 42.3325651,
"Longitude": -71.1140136,
"Company_Type": {}
},
{
"Hospital_Name": "Sample Brookline Company",
"Address_Line_1": "442 Oak Boulevard",
"Address_Line_2": "",
"City": "Brookline",
"State": "MA",
"Latitude": 42.1231213,
"Longitude": -71.5454354,
"Company_Type": {}
}
]
},
{
"ID": "id2",
"Display_Value": "A",
"Companies": [ { "more companies that match this grouping" }]
}
]
}
我尝试了什么
从Company_Types
获取唯一facetItems
:
_.chain(self.results()).map(function (f) { return f.Hospital_Type })
.flatten().uniq(function (f_ht) { return f_ht.ID }).value();
答案 0 :(得分:0)
这是一个工作示例
var
facetItems = [
{
"Company_Name": "Sample Brookline Company",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Brookline",
"State": "MA",
"Latitude": 42.3325651,
"Longitude": -71.1140136,
"Company_Type": [
{
"ID": "id2",
"Display_Value": "A"
},
{
"ID": "id4",
"Display_Value": "B"
}
]
},
{
"Company_Name": "Sample Everett Company",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Everett",
"State": "MA",
"Latitude": 42.4030189,
"Longitude": -71.0454874,
"Company_Type": [
{
"ID": "id1",
"Display_Value": "G"
},
{
"ID": "id2",
"Display_Value": "A"
},
{
"ID": "id3",
"Display_Value": "E"
},
{
"ID": "id4",
"Display_Value": "B"
},
{
"ID": "id5",
"Display_Value": "C"
}
]
},
{
"Company_Name": "Sample Wakefield Company 1",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091051,
"Longitude": -71.0675369,
"Company_Type": [
{
"ID": "id1",
"Display_Value": "G"
},
{
"ID": "id2",
"Display_Value": "A"
},
{
"ID": "id3",
"Display_Value": "E"
},
{
"ID": "id4",
"Display_Value": "B"
},
{
"ID": "id5",
"Display_Value": "C"
}
]
},
{
"Company_Name": "Sample Wakefield Company 2",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091059,
"Longitude": -71.0675379,
"Company_Type": [
{
"ID": "id1",
"Display_Value": "G"
},
{
"ID": "id2",
"Display_Value": "A"
},
{
"ID": "id3",
"Display_Value": "E"
},
{
"ID": "id4",
"Display_Value": "B"
}
]
},
{
"Company_Name": "Sample Wakefield Company 3",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091089,
"Longitude": -71.0675399,
"Company_Type": [
{
"ID": "id3",
"Display_Value": "E"
},
{
"ID": "id5",
"Display_Value": "C"
}
]
},
{
"Company_Name": "Sample Wakefield Company 4",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091101,
"Longitude": -71.0675401,
"Company_Type": [
{
"ID": "id1",
"Display_Value": "G"
},
{
"ID": "id2",
"Display_Value": "A"
},
{
"ID": "id3",
"Display_Value": "E"
}
]
},
{
"Company_Name": "Sample Wakefield Company 5",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091131,
"Longitude": -71.0675431,
"Company_Type": [
{
"ID": "id3",
"Display_Value": "E"
},
{
"ID": "id4",
"Display_Value": "B"
},
{
"ID": "id5",
"Display_Value": "C"
}
]
},
{
"Company_Name": "Sample Wakefield Company 6",
"Address_Line_1": "242 Oak Boulevard",
"Address_Line_2": "",
"City": "Wakefield",
"State": "MA",
"Latitude": 42.5091151,
"Longitude": -71.0675451,
"Company_Type": [
{
"ID": "id1",
"Display_Value": "G"
},
{
"ID": "id2",
"Display_Value": "A"
},
{
"ID": "id4",
"Display_Value": "B"
},
{
"ID": "id5",
"Display_Value": "C"
}
]
}
]
;
var companyTypes = [
{ "ID": "id1", "Display_Value": "G" },
{ "ID": "id2", "Display_Value": "A" },
{ "ID": "id3", "Display_Value": "E" },
{ "ID": "id4", "Display_Value": "B" },
{ "ID": "id5", "Display_Value": "C" }
]
var finalResult = _.map(companyTypes,function(a){
a.Companies = _.filter(facetItems, function(b){
return _.findWhere(b.Company_Type,{ID:a.ID});
});
return a;
})
document.getElementById('result').innerText = JSON.stringify(finalResult)

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="result">
</div>
&#13;
基本上我会映射所有公司类型,为每个公司添加房产公司并寻找相应的公司