json数组存储了两个对象,要怎么遍历

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json数组存储了两个对象,要怎么遍历相关的知识,希望对你有一定的参考价值。

"all":["menuid":10,"pmenuid":0,"url":"javascript:void(0);","menuname":"系统管理","isnode":0,"sort":1,"level":1,"enable":0,"menuid":1010,"pmenuid":10,"url":"/view/yhgl/yhcx.jsp","menuname":"用户管理","isnode":1,"sort":1,"level":2,"enable":0,"menuid":1011,"pmenuid":10,"url":"/view/yhgl/yhxz.jsp","menuname":"角色管理","isnode":1,"sort":2,"level":2,"enable":0,"menuid":11,"pmenuid":0,"url":"javascript:void(0);","menuname":"文章管理","isnode":0,"sort":2,"level":1,"enable":0,"menuid":1110,"pmenuid":11,"url":"bus_article.html","menuname":"文章管理","isnode":1,"sort":1,"level":2,"enable":0,"menuid":1111,"pmenuid":11,"url":"bus_article_check.html","menuname":"文章审核","isnode":1,"sort":2,"level":2,"enable":0,"menuid":1112,"pmenuid":11,"url":"bus_article_seq.html","menuname":"文章排序","isnode":1,"sort":3,"level":2,"enable":0,"menuid":12,"pmenuid":0,"url":"javascript:void(0);","menuname":"标签管理","isnode":0,"sort":3,"level":1,"enable":0,"menuid":1210,"pmenuid":12,"url":"/view/bqgl/bqgl.jsp","menuname":"标签管理","isnode":1,"sort":1,"level":2,"enable":0,"menuid":13,"pmenuid":0,"url":"javascript:void(0);","menuname":"工作统计","isnode":0,"sort":4,"level":1,"enable":0,"menuid":1310,"pmenuid":13,"url":"bus_count.html","menuname":"工作统计","isnode":1,"sort":1,"level":2,"enable":0],
"user":["menuid":10,"pmenuid":0,"menuname":"系统管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1010,"pmenuid":0,"menuname":"用户管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1011,"pmenuid":0,"menuname":"角色管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":11,"pmenuid":0,"menuname":"文章管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1110,"pmenuid":0,"menuname":"文章管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1111,"pmenuid":0,"menuname":"文章审核","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1112,"pmenuid":0,"menuname":"文章排序","isnode":0,"sort":0,"level":0,"enable":0,"menuid":12,"pmenuid":0,"menuname":"标签管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1210,"pmenuid":0,"menuname":"标签管理","isnode":0,"sort":0,"level":0,"enable":0,"menuid":13,"pmenuid":0,"menuname":"工作统计","isnode":0,"sort":0,"level":0,"enable":0,"menuid":1310,"pmenuid":0,"menuname":"工作统计","isnode":0,"sort":0,"level":0,"enable":0]

在jquery里面,分别要取到“all”的所有menuid和menuname,也要取到“user”的所有menuid和menuname

for(var i in json.all)
    console.log(json.all[i].menuid + ": " + json.all[i].menuname);
for(var i in json.user)
    console.log(json.user[i].menuid + ": " + json.user[i].menuname);

或者

for(var key in json) 
    for(var i in json[key])
        console.log(json[key][i].menuid + ": " + json[key][i].menuname;

里面的json就是你的json对象。如果是文本格式,就先用JSON.parse转化一下

参考技术A for(var key in json)//key为键值 ,json[key]为value值

循环遍历两个带有布尔条件的 JSON 数组 [Angular 10]

【中文标题】循环遍历两个带有布尔条件的 JSON 数组 [Angular 10]【英文标题】:Loop Through Two JSON Arrays With a Boolean Condition [Angular 10] 【发布时间】:2020-11-29 15:33:54 【问题描述】:

我有两个来自外部的 JSON 数组,我想在 Angular 中循环,但我不确定该怎么做。

对象中的第一个数组看起来像 ViewAttributes.FunctionNames,第二个数组是 ViewAttributes.FunctionActive。

现在我正在使用正确的菜单,但我的目标是当每个值的布尔位置为 true,而不是 false 时,在菜单选项中显示正确的 FunctionNames 值。我需要改变什么?

我的代码如下:

view.component.html

  <mat-menu #contextMenu="matMenu" #contextMenu2="matMenu">
    <ng-template matMenuContent let-action="action">
      <button mat-menu-item (click)="onContextMenuAction(action)">FunctionNames</button>
    </ng-template>
  </mat-menu>

view.component.ts

      // Right Click Context Menu for View Data Table

    onContextMenu(event: MouseEvent, action: ViewDataSource) 
      event.preventDefault();
      this.contextMenuPosition.x = event.clientX + 'px';
      this.contextMenuPosition.y = event.clientY + 'px';
      this.contextMenu.menuData =  action: action ;
      this.contextMenu.menu.focusFirstItem('mouse');
      this.contextMenu.openMenu();
      console.log(this.FunctionNames);
    

    onContextMenuAction(action: ViewDataSource ) 
      console.log(action);
      console.log(parseInt(action[0]));
      console.log(parseInt(action[3]));

      // this.launchService.launchAction(this.line.targetActionTag.value, this.line.targetActionType);
      // tslint:disable-next-line: radix
      this.launchService.launchAction(parseInt(action[0]), parseInt(action[3]));
    //   debugger;

    

我的两个 JSON 数组对象看起来像:

ViewAttributes.FunctionNames = [Value1, Value2, Value3, ....]

ViewAttributes.FunctionActive = [true,false,true, ...]

【问题讨论】:

【参考方案1】:

如果您想根据FunctionActive 数组中相应索引处的布尔值显示FunctionNames。那么下面的代码可能会对您有所帮助

ViewAttributes.FunctionNames =ViewAttributes.FunctionNames.filter((each,index)=>return ViewAttributes.FunctionActive [index])

这将根据FunctionActive 数组中的布尔值过滤数组。

【讨论】:

你能帮我解决这个问题吗? @DhirajSingh ***.com/questions/63333960/… 回答了问题。希望它能解决您的问题***.com/questions/63333960/…

以上是关于json数组存储了两个对象,要怎么遍历的主要内容,如果未能解决你的问题,请参考以下文章

JS 两个数组对象的遍历去重

怎样用for循环动态遍历json数组

如何遍历一个对象数组并创建一个 json 对象?

java中把json怎么转换成数组

求jsp json数组遍历方法

js的Dom对象集合循环遍历过程中数组长度发生变化,小白不懂,求大神指教