简体   繁体   English

Javascript-存储父母和孩子的价值观

[英]Javascript - Storing Parent and Child Values

I need some help... using Javascript I'm trying to create a parent-child relationship for lists and I'm not sure how to achieve this. 我需要一些帮助...使用Javascript,我试图为列表创建父子关系,但不确定如何实现。

The objective is to have a structure depicted below: 目的是要具有如下所示的结构:

ParentObject 1 父对象1

  1. PO1 List item PO1清单项目
  2. PO1 List item PO1清单项目
  3. PO1 List item PO1清单项目
  4. PO1 List item PO1清单项目

ParentObject 2 父对象2

  1. P02 List item P02清单项目
  2. P02 List item P02清单项目
  3. P02 List item P02清单项目
  4. P02 List item P02清单项目

... ...

I'm not looking for someone to code it out for me, I'm just looking for guidance from the community. 我不是在寻找为我编写代码的人,而是在寻找社区的指导。

You can create an object with an array as a property. 您可以创建一个具有数组作为属性的对象。

So 所以

var parent = {
    childlist = [1,2,3,4,5];

}

You can then access the child elements with parent.childlist[index] to get the child at the index position of the list. 然后,您可以使用parent.childlist[index]访问子元素,以使子元素位于列表的索引位置。 You can add a new item x to the list with parent.childlist.push(x) 您可以使用parent.childlist.push(x)将新项x添加到列表中

if you want to simplify creating this multiple times you can use a constructor, like this. 如果您想简化多次创建操作,可以使用类似这样的构造函数。

var Parent = function(){
    this.childlist = [];
}

var parent1 = new Parent();

var parent2 = new Parent();

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

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