用Javascript合并数组中的链接数据 [英] Merging linked Data in Array in Javascript

查看:46
本文介绍了用Javascript合并数组中的链接数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的任务,可以在JSON中重新排列几个数组,因此ractive.js可以更好地处理它.但是我有点生气,结果不是特别令人满意.

我的初始数组的示例:

  [{"_id":1"type":"person",名称":汉斯","WorksFor":["3","4"],},{"_id":2"type":"person",名称":"Michael","WorksFor":["3"],},{"_id":3,"type":部门",名称":市场营销"},{"_id":4"type":部门",名称":销售"},{"_id":5"type":"person",名称":克里斯",效劳于": [],}] 

因此,对于给定的部门,我想要一种主动的方法,向我提供在该部门工作的所有人员(及其工作部门的列表).像这样:

  [{"_id":1"type":"person",名称":汉斯","WorksFor":["3","4"],可读":[营销",销售"]},{"_id":2"type":人",名称":"Michael","WorksFor":["3"],可读":[销售"]}] 

以某种方式变为现实的功能与此类似:

  function imsorryforthis(){让输出= [];让tempdocs = this.get('docs');//由于这种情况是在主动方法中发生的,//this.get"对于绑定是必要的for(var i = 0; i< tempdocs.length; i ++){if(_.contains(tempdocs [i] .WorksFor,namedDepartment)){//我在这里使用了underscore.js让collectedDepartmentData = [];如果(tempdocs [i] .WorksFor.length> 0){for(var f = 0; f< tempdocs [i] .WorksFor.length; f ++){for(var g = 0; g< tempdocs.length; g ++){如果(tempdocs [i] .WorksFor [f] == tempdocs [g] ._ id){让actualDepartmentData = {};actualDepartmentData = tempdocs [g];collectedDepartmentData.push(actualDepartmentData);}}}}tempdocs [i] .Readable = collectedDepartmentData;output.push(tempdocs [i]);}}返回输出;} 

我也将其放在小提琴中,以使其更易读.>

由于这种怪兽确实起作用了(我很惊讶),所以感觉就像用右手划过头擦了你的左耳(同时被一群绝望的数学家大喊大叫).

也许任何人都知道一种更美观,更聪明的方法(或一种编译JavaScript的方法,因此这再也看不到了.)

解决方案

构建地图 department_id =>部门名称首先:

 让部门= {};对于(让x的数据){如果(x.type ==='部门'){部门[x._id] = x.Name;}} 

然后,遍历Persons并从该地图填充 Readable 数组:

  for(让x的数据){如果(x.type ==='person'){x.Readable = x.WorksFor.map(w =>部门[w]);}} 

最后,提取特定部门的人员:

  personsInSales = data.filter(x =>x.type ==='person'&&x.WorksFor.includes('3')); 

I have a simple task of rearranging a couple of Arrays in a JSON, so ractive.js can handle it better. But I got carried away a bit, and the outcome was not particularly satisfactory.

An example of my initial Array:

[{
  "_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": 5,
  "type": "person",
  "Name": "Chris",
  "WorksFor": [],
}]

So with a given Department I wanted a method in ractive to give me all Persons who work in this Department (with a list of Departments they work for). Something like:

[{
  "_id": 1,
  "type": "person",
  "Name": "Hans",
  "WorksFor": ["3", "4"],
  "Readable": ["Marketing", "Sales"]
}, {
  "_id": 2,
  "type": "person",
  "Name": "Michael",
  "WorksFor": ["3"],
  "Readable": ["Sales"]
}]

The function that somehow came to life was similar to this:

function imsorryforthis() {
  let output = [];
  let tempdocs = this.get('docs'); //as this happens in a ractive method, 
//"this.get" is neccesary for binding 
  for (var i = 0; i < tempdocs.length; i++) {
    if (_.contains(tempdocs[i].WorksFor, givenDepartment)) { //I used underscore.js here
      let collectedDepartmentData = [];
      if (tempdocs[i].WorksFor.length > 0) {
        for (var f = 0; f < tempdocs[i].WorksFor.length; f++) {
          for (var g = 0; g < tempdocs.length; g++) {
            if (tempdocs[i].WorksFor[f] == tempdocs[g]._id) {
              let actualDepartmentData = {};
              actualDepartmentData = tempdocs[g];
              collectedDepartmentData.push(actualDepartmentData);
            }
          }
        }
      }
      tempdocs[i].Readable = collectedDepartmentData;
      output.push(tempdocs[i]);
    }
  }
  return output;
}

I've put it in a Fiddle as well to make it better readable.

Due to the fact that somehow this monstrosity does work (I was astonished myself), it feels like scratching your left ear with your right hand over your head (while being constantly shouted at by a group of desperate mathematicians).

Maybe anybody knows a more presentable and smarter approach (or a way to compile JavaScript so this never sees the light of day again).

解决方案

Construct a map department_id => department_name first:

let departments = {};

for (let x of data) {
    if (x.type === 'department') {
        departments[x._id] = x.Name;
    }
}

Then, iterate over Persons and populate Readable arrays from that map:

for (let x of data) {
    if (x.type === 'person') {
        x.Readable = x.WorksFor.map(w => departments[w]);
    }
}

Finally, extract Persons for the specific Department:

personsInSales = data.filter(x => 
    x.type === 'person' && x.WorksFor.includes('3'));

这篇关于用Javascript合并数组中的链接数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆