Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 角度7嵌套,用于将父值用作子值的索引_Javascript_Arrays_Angular_Nested Loops_Ngfor - Fatal编程技术网

Javascript 角度7嵌套,用于将父值用作子值的索引

Javascript 角度7嵌套,用于将父值用作子值的索引,javascript,arrays,angular,nested-loops,ngfor,Javascript,Arrays,Angular,Nested Loops,Ngfor,在角度为2+的情况下可以这样做吗 假设我有以下对象: myParent = [{id: 1, code: 'code1', title: 'parentTitle1'}, {id: 2, code: 'code2', title: 'parentTitle2'}]; myChild = {code1: [{id: 1, title: 'childTitle1'}, {id: 2, title: 'childTitle2'}], code2: [{id: 4, title: 'childTit

在角度为2+的情况下可以这样做吗

假设我有以下对象:

 myParent = [{id: 1, code: 'code1', title: 'parentTitle1'}, {id: 2, code: 'code2', title: 'parentTitle2'}];
 myChild = {code1: [{id: 1, title: 'childTitle1'}, {id: 2, title: 'childTitle2'}], code2: [{id: 4, title: 'childTitle1'}]
我想迭代第一个,然后显示第二个的项目,使用父对象的“code”值作为索引:

<h3 *ngFor="let parent of myParent">{{parent .title}} 
  <br>
  <span *ngFor="let child of myChild[parent.code]"> {{child.title}} </span>
  <br>
</h3>
{{parent.title}

{{child.title}
我在控制台上没有收到任何错误,但是子对象“for”没有显示任何内容。我曾经在AngularJS上这样做过,但不确定是否可以在NG7中这样做。这对你不管用吗

我尝试使用这些数据:

  myParent = [{id: 1, code: 'code1', title: 'parentTitle1'}, {id: 2, code: 'code2', title: 'parentTitle2'}];
 myChild = {code1: [{id: 1, title: 'childTitle1'}, {id: 2, title: 'childTitle2'}], code2: [{id: 4, title: 'childTitle1'}]};
还有html

<h3 *ngFor="let parent of myParent">{{parent .title}} 
  <br>
  <span *ngFor="let child of myChild[parent.code]"> {{child.title}} </span>
  <br>

</h3>
{{parent.title}

{{child.title}

stackblitz正在按你的计划进行。您可以再分享一些代码吗,因为数据可能不匹配。

myChild
是一个对象。。。。您不能对其进行迭代objects@Nickolaus迭代不是在myChild上,而是在myChild属性代码上,它是arrayright。。。但是
myChild={code1:[{id:1,title:childTitle1}
…childTitle1不是字符串;)…它是一个属性检查stackblitz。对象的属性可以作为数组键访问。好的,我知道了为什么要进行向下投票。你能再检查一下吗?也许你可以将stackblitz的相关部分添加到你的答案中,这将大大提高它的质量。我没有这样做,因为我唯一的修复方法是将标题设置为字符串,这在本例中并不是真正的问题。您的答案是正确的,我仔细检查了代码,有一种方法对原始对象进行了深度克隆,但出现了错误,我记录了错误的数组,因此,我的错误!谢谢!