我的 JSON 数据如下:
"data": [
{
"docName": "hpc",
"station": "delhi"
},
{
"docName": "Dr. Sharma",
"station": "chd"
},
{
"docName": "ansh",
"station": "chandigrag"
},
{
"docName": "Bhupi",
"station": "jhujhar nagar"
},
{
"docName": "varun",
"station": "punjab"
},
{
"docName": "varun",
"station": "delhi"
},
{
"docName": "Rk",
"station": "delhi"
},
{
"docName": "Rk",
"station": "delhi"
}
]
这有多个站点和独特的医生姓名,我想将其列为
data: {
delhi: {
0 : hpc,
1 : varun,
2 : RK,
3 : ansh
},
chandigrah: {
0 : ansh,
1: ansh,
},
stationName : {
0 : doctorName,
1 : doctorNam
}
}
像这样,我需要独特的电台作为标题和这些标题中的名称 我正在尝试制作字典???///
最佳答案
下面是简单的底层编程,我们可以对其进行更好的调整以获得良好的结果。 还分享给你Stackblitz link
var button = document.getElementById('testButton');
button.addEventListener('click', test);
var obj = [{ s_no: "1" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc1" ,station: "chennai" },
{ s_no: "2" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc2" ,station: "madurai" },
{ s_no: "3" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc3" ,station: "trichy" },
{ s_no: "4" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc4" ,station: "kovai" },
{ s_no: "5" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc5" ,station: "chennai" },
{ s_no: "6" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc6" ,station: "madurai" },
{ s_no: "7" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc7" ,station: "chennai" },
{ s_no: "8" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc8" ,station: "madurai" },
{ s_no: "9" ,userId: "1" ,name: "RAhul" ,designation: "MD" ,docName: "doc9" ,station: "chennai" }];
function test(){
var resultObj = {};
for(var i=0; i< obj.length; i++){
var station = obj[i].station
var result = obj.filter(_obj => {
return _obj.station === station;
});
let docArray = result.map(a => a.docName);
resultObj[station] = docArray;
}
console.log(resultObj);
}
<div id="app"></div>
<button id="testButton">Click me</button>
关于javascript - 在js中制作动态字典,其中包含唯一的 header 值和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59901867/