简体   繁体   English

将对象的新对象作为 arrays 添加到数组

[英]Adding new objects of objects as arrays to an array

I've being scratching my day all day long to figure this out.我整天都在抓挠我的一天来解决这个问题。 New to javascript I need to make a WebApp using GAS for my team and I'm stuck in constructing my objects / arrays. javascript 的新手我需要为我的团队使用 GAS 制作一个 WebApp,但我一直在构建我的对象 / arrays。

I have 2 data sources (google sheets).我有 2 个数据源(谷歌表)。 The first data source is a classic 2D array called dbProject which gives following structure when consoled out: {{…},{…},{…},{…},…}.第一个数据源是一个经典的 2D 数组,称为dbProject ,它在输出时给出以下结构:{{...},{...},{...},{...},...}。 project[0] for example returns {id: “id0”, projectCreatedBy: “fred”} . project[0]例如返回{id: “id0”, projectCreatedBy: “fred”}

Each of these projects has a least one phase minimum, but can have more.这些项目中的每一个都有至少一个阶段的最小值,但可以有更多。 Each phase is structured similarly.每个阶段的结构相似。 In this example, a phase contains logistics and team information.在此示例中,一个阶段包含后勤和团队信息。

The second datasource dbPhase records project events relevant to 1 project's phase, such as adding a new phase to a project, adding team members or logistic info to a phase, etc… This data source is less structured then the first one and is similar to this (sorry for the bad formatting):第二个数据源dbPhase记录与 1 个项目阶段相关的项目事件,例如向项目添加新阶段、向阶段添加团队成员或后勤信息等……该数据源的结构不如第一个数据源,与此类似(抱歉格式错误):

Id* % projectId % phaseId % typeOfEvent % pty1 % pty2 % pty3 % pty4 Id* % projectId % phaseId % typeOfEvent % pty1 % pty2 % pty3 % pty4

0 % 0 % 0 % phase % title00 % aDate00 % bDate00 % status0 0 % 0 % 0 % 阶段 % title00 % aDate00 % bDate00 % status0

1 % 0 % 0 % team % fred % mng % fred@ % empty % empty 1 % 0 % 0 % 团队 % 弗雷德 % mng % 弗雷德@ % 空 % 空

2 % 0 % 0 % team % nick % wkr % nick@ % empty % empty 2 % 0 % 0 % 团队 % nick % wkr % nick@ % empty % empty

3 % 1 % 0 % phase % title10 % aDate10 % bDate10 % status0 3 % 1 % 0 % 阶段 % title10 % aDate10 % bDate10 % status0

4 % 1 % 0 % logistics % ny % US % NA % empty % empty 4 % 1 % 0 % 物流 % ny % 美国 % NA % 空 % 空

5 % 1 % 1 % phase % title11 % date11 % date11 % status0 5 % 1 % 1 % 阶段 % title11 % date11 % date11 % status0

6 % 0 % 1 % logistics % par % FR % EU % empty % empty 6 % 0 % 1 % 物流 % par % FR % EU % empty % empty

7 % 1 % 1 % team % mike % hr % mike@ % empty % empty 7 % 1 % 1 % 团队 % 麦克 % 小时 % 麦克@ % 空 % 空

The data is being added to the table on a time basis (most recent event on last line), meaning that it cannot be sorted to rearrange it.数据是按时间添加到表中的(最后一行的最新事件),这意味着无法对其进行排序以重新排列它。 Each line representing an event for a given phase of a given project, I've decided to structure this by creating an object for each 'typeOfEvent' code.每行代表给定项目的给定阶段的事件,我决定通过为每个“typeOfEvent”代码创建一个 object 来构建它。 My objects are coded as follow:我的对象编码如下:

function phase(){
this.title = ‘ ’ ;
this.aDate = ‘ ’ ;
this.bDate = ‘ ’ ;
this.status = ‘ ’ ;
}


function logistics (){
phase.call(this);
    this.address = ‘ ’;
    this.city = ‘ ’;
    this.continent = ‘ ’;
}
logistics.prototype = Object.create(phase.prototype);
logistics.prototype.constructor = logistics;


function team(){
    phase.call(this);
    this.name = ‘ ’;
    this.role = ‘ ’;
this.email = ‘ ’;
}
team.prototype = Object.create(project.prototype);
team.prototype.constructor = team;

Inside a dbPhase.map() , I'm reading the typeOfEvent code and call dynamically the suitable object and property (one of the above), and then 'add' it to dbProject as follow:dbPhase.map()中,我正在读取 typeOfEvent 代码并动态调用合适的 object 和属性(上述之一),然后将其“添加”到dbProject如下:

dbPhase.map(function (row){

     //oObject is one of the above structure
     eval("var myObject = new " + oObject);

     for (var property in myObject) {
              eval('myObject.' + property + " = row[" + property + "]");  
     };

     dbProject[idProject]][typeOfEvent] = myObject

}

This runs ok as long as the structured objects don't come to add up, in which case they override the previous ones (for example id* 2 and 3: adding typeOfEvent for same project/phase patern. This will only keep the info of id* 3; id* 3 and 5: adding a new phase. Again this will only retain info from id*5; …).只要结构化对象不相加,就可以正常运行,在这种情况下,它们会覆盖以前的对象(例如 id* 2 和 3:为相同的项目/阶段模式添加 typeOfEvent。这只会保留id* 3; id* 3 and 5: 添加一个新阶段。同样,这只会保留来自 id*5; ...) 的信息。

My query: I wish to have a structure like this:我的查询:我希望有这样的结构:

dbProjects(idProject).phase(idPhase).typeOfEvent.property? dbProjects(idProject).phase(idPhase).typeOfEvent.property?

ex: dbProjects(0).phase(1).logistic.pt1 to return “ny” ex: dbProjects(0).phase(0).team.pt2 to return an array of {fred@;例如:dbProjects(0).phase(1).logistic.pt1 返回“ny” 例如:dbProjects(0).phase(0).team.pt2 返回 {fred@; 数组nick@}缺口@}

I've been using javascript for a month only now and don't yet master all differences between Arrays, Objects, etc... Been trying various things all day long (push, assign, brackets, parenthesis, ...) and desperately searching on the web without success (perhaps not using the correct wording).我已经使用 javascript 一个月了,还没有掌握 Arrays、对象等之间的所有差异......整天都在尝试各种事情(推、分配、括号、括号......)并且拼命在 web 上搜索没有成功(可能没有使用正确的措辞)。 I've been looking at following posts as well, figuring that the answers would hide in there.我也一直在查看以下帖子,认为答案会隐藏在那里。 I have no clue however on how arrange these solutions for my case (i keep thinking that it would be much easier if dbPhase table could be sorted - but it can't).但是,我不知道如何为我的案例安排这些解决方案(我一直认为如果可以对 dbPhase 表进行排序会更容易 - 但它不能)。

Angular2, Typescript: Add an object to an object array of an object array of an object Angular2, Typescript: Add an object to an object array of an object array of an object

Add new object to another object's object 将新的 object 添加到另一个对象的 object

I could perhaps make quick and dirty loops and sortings to dynamically push the results?我也许可以进行快速而肮脏的循环和排序以动态推送结果? But ideally I would rather like a clean code.但理想情况下,我更喜欢干净的代码。 Your help would be more than welcome.您的帮助将非常受欢迎。 Thanks in advance for your tips.提前感谢您的提示。

based on基于

dbProjects(idProject).phase(idPhase).typeOfEvent.property

It seems you want to keep it as nested objects rather than add an inner array?似乎您想将其保留为嵌套对象而不是添加内部数组?

So dbProject would appear as所以 dbProject 会显示为

const dbProject = {
  "id0": {
    phase: {}, 
    logistics: {}, 
    team: {}
  },
  "id1": {
    phase: {}, 
    logistics: {}, 
    team: {}
  },
  ...
}

In that case在这种情况下

dbProject[idProject]][typeOfEvent] = myObject

should be rewritten as应该改写为

//first line not needed if hard coded as default
if(!dbProject[idProject]][typeOfEvent]) dbProject[idProject]][typeOfEvent] = {};
dbProject[idProject]][typeOfEvent][yourKey] = myObject

yourKey being how you want to distinguish that specific log from others of the same type. yourKey 是您希望将该特定日志与其他相同类型的日志区分开来的方式。 Does each new log in the second datasource have a unique id (one that refers to the log, not the project id)?.第二个数据源中的每个新日志是否都有唯一的 id(指的是日志,而不是项目 id)? That would be a good key to use, otherwise, if you are just adding numerical keys, it would make more sense to use an array and rewrite as那将是一个很好的键,否则,如果您只是添加数字键,则使用数组并将其重写为更有意义

//first line not needed if hard coded as default
if(!dbProject[idProject]][typeOfEvent]) dbProject[idProject]][typeOfEvent] = [];
dbProject[idProject]][typeOfEvent].push(myObject);

The resulting dbProject now looking like生成的 dbProject 现在看起来像

const dbProject = {
  "id0": {
    phase: [], 
    logistics: [], 
    team: []
  },
  "id1": {
    phase: [], 
    logistics: [], 
    team: []
  },
  ...
}

and each log accessed as并且每个日志访问为

dbProject[idProject][typeOfEvent][index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM