Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反向映射JSON数据?_Javascript_Arrays_Json_Mapping - Fatal编程技术网

Javascript 反向映射JSON数据?

Javascript 反向映射JSON数据?,javascript,arrays,json,mapping,Javascript,Arrays,Json,Mapping,我被困在一个可能很简单的任务上,但找不到任何解决办法。 我有一些JSON数据——比如说: [{ "_id": 1, "type": "person", "Name": "Hans", "WorksFor": ["3", "4"] }, { "_id": 2, "type": "person", "Name": "Michael", "WorksFor": ["3"] }, { "_id": 3, "type": "department", "Name"

我被困在一个可能很简单的任务上,但找不到任何解决办法。 我有一些JSON数据——比如说:

[{
  "_id": 1,
  "type": "person",
  "Name": "Hans",
  "WorksFor": ["3", "4"]
}, {
  "_id": 2,
  "type": "person",
  "Name": "Michael",
  "WorksFor": ["3"]
}, {
  "_id": 3,
  "type": "department",
  "Name": "Marketing"
}, {
  "_id": 4,
  "type": "department",
  "Name": "Sales"
}]
据我所知,使用部门地图阵列将所有人员和他们工作的部门放在一起非常简单。 然后,我可以将相应的部门映射到该人员,并收到如下信息:

[{
  "_id": 1,
  "type": "person",
  "Name": "Hans",
  "WorksFor": ["3", "4"],
  "Readable": ["Marketing", "Sales"]
}, {
  "_id": 2,
  "type": "person",
  "Name": "Michael",
  "WorksFor": ["3"],
  "Readable": ["Sales"]
}]
但对于另一个界面,我需要“相反方向”的数据


有没有合适的方法来实现这种结构?两天的尝试没有让我有任何进展…

这里有一种方法可以让你获得第一个映射。我已经添加了一些评论,这样您就可以跟进,我希望您能够找到第二个问题的答案

// First, let's get just the items in this array that identify persons
// I've called this array "data"
data.filter(x => x.type === 'person') 
    // Now let's map over them
    .map(person =>
      // We want all of the data associated with this person, so let's
      // use Object.assign to duplicate that data for us
      Object.assign({}, person, {
          // In addition, we want to map the ID of the WorksFor array to the Name
          // of the corresponding department. Assuming that the _id key is unique,
          // we can due this simply by mapping over the WorksFor array and finding
          // those values within the original array.
          Readable: person.WorksFor.map(wfId =>
             // Notice here the parseInt. This will not work without it due to
             // the type difference between WorksFor (string) and _id (integer)
             data.find(d => d._id === parseInt(wfId)).Name
          )
      })
);

您可以尝试以下方法:

var data=[{{u id:1,“类型”:“人员”,“姓名”:“Hans”,“工作人员”:[“3”,“4”]},{{u id:2,“类型”:“人员”,“姓名”:“Michael”,“工作人员”:[“3”]},{{u id:3,“类型”:“部门”,“姓名”:“营销”},{u id:4,“类型”:“部门”,“姓名”:“销售”}]
变量ignoreDept=['人'];
var结果=数据.reduce(函数(p,c,i,a){
if(信号部门索引(c.type)<0){
c、 员工=a.reduce(功能(arr、emp){
if(emp.WorksFor&&emp.WorksFor.indexOf(c.\u id.toString())>-1){
arr.push(emp.Name)
}
返回arr;
},[]);
p、 推(c);
}
返回p;
}, []);

console.log(result)
使用
Array.prototype.filter()和
Array.prototype.forEach()函数的解决方案:

var数据=[{{u id:1,“类型”:“人员”,“姓名”:“Hans”,“工作人员”:[“3”,“4”]},{{u id:2,“类型”:“人员”,“姓名”:“Michael”,“工作人员”:[“3”]},{{u id:3,“类型”:“部门”,“姓名”:“营销”},{u id:4,“类型”:“部门”,“姓名”:“销售”},
//获取部门和员工(人)的分离“名单”
deps=data.filter(函数(o){returno.type==“department”;}),
persons=data.filter(函数(o){returno.type==“person”;});
副首席财务官(职能(d){
d['employees']=d['employees'.];
人员。forEach(职能(p){
if(p.WorksFor.indexOf(String(d._id))!=-1{//检查员工和部门之间的'id'是否一致
d[“员工”].推送(p.Name);
}
});
});

控制台日志(deps)可以对每个数组使用哈希表和单个循环

方法:

  • 用于迭代数组并返回结果
  • 对于循环内部数组
    工作
  • 要生成没有任何原型的对象
  • 其他一些模式,比如
    散列上的闭包
  • 用于检查错误值并将对象作为默认值

    hash[b] = hash[b] || { _id: b, employees: [] };
    
var data=[{{u-id:1,类型:“person”,名称:“Hans”,工作对象:[3,4]},{u-id:2,类型:“person”,名称:“Michael”,工作对象:[3]},{u-id:3,类型:“department”,名称:“Marketing”},{u-id:4,类型:“department”,名称:“Sales”},
结果=数据.reduce(函数(散列){
返回函数(r,a){
如果(a.type==‘person’){
a、 forEach的工作(功能(b){
hash[b]=hash[b]|{_id:b,雇员:[]};
hash[b].employees.push(a.Name);
});
}
如果(a.type==‘部门’){
hash[a._id]=hash[a._id]|{_id:b,雇员:[]};
散列[a.\u id].type=a.type;
散列[a.\u id].Name=a.Name;
r、 push(散列[a._id]);
}
返回r;
};
}(Object.create(null)),[]);
控制台日志(结果)
.as控制台包装{最大高度:100%!重要;顶部:0;}
var数据=[{{u id:1,“类型”:“人员”,“姓名”:“Hans”,“工作人员”:[“3”,“4”]},{{u id:2,“类型”:“人员”,“姓名”:“Michael”,“工作人员”:[“3”]},{u id:3,“类型”:“部门”,“姓名”:“营销”},{u id:4,“类型”:“部门”,“姓名”:“销售”};
var部门=[],
人员=[];
data.forEach(e=>{
如果(如类型==“人”){
人。推(e);
}否则,如果(例如类型==“部门”){
部门.推动(e);;
e、 雇员=[];
}
});
departments.forEach(d=>{
var workers=persons.filter(p=>p.WorksFor.indexOf(d._id.toString())>-1)
/*.map(p=>p.Name)*///如果只需要名称而不需要完整的“person”,请添加此选项
d、 雇员=d.employees.concat(工人);
});
log(JSON.stringify(departments,null,4))
var数据=[{{u id:1,“类型”:“人员”,“姓名”:“Hans”,“工作人员”:[“3”,“4”]},{{u id:2,“类型”:“人员”,“姓名”:“Michael”,“工作人员”:[“3”]},{u id:3,“类型”:“部门”,“姓名”:“营销”},{u id:4,“类型”:“部门”,“姓名”:“销售”};
var-dep={};
data.forEach(e=>(e.type=='person'&&e.WorksFor.forEach(d=>dep[d]?dep[d].push(e.Name):dep[d]=[e.Name]);
data.forEach(e=>(e.type=='department'&(e.employees=dep[e.[u id]| |[]);
data=data.filter(e=>e.type=='department');

控制台日志(数据)你写过代码吗?
hash[b] = hash[b] || { _id: b, employees: [] };