【问题标题】:How get all 'name' values in a json array? [duplicate]如何获取 json 数组中的所有“名称”值? [复制]
【发布时间】:2020-07-30 08:49:28
【问题描述】:

我正在 Angular 中制作应用程序。我有一个 Json 文件。 如何获取数组中的所有“名称”值? 假设在输出中我可以得到类似的东西:

['Sony', 'Apple', 'Sony']

json:

[
  {
    "Data": [
        {
            "name": "Sony",
            "color": "black"
        }            
    ] 
},
{
    "Data": [
        {
            "name": "Apple",
            "color": "white"
        }            
    ] 
},
{
    "Data": [
        {
            "name": "Sony",
            "color": "red"
        }            
    ] 
  }
]

app.component:

import { Component, OnInit } from '@angular/core';
import resData from '../assets/result.json';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.pug',
 styleUrls: ['./app.component.styl']
})

export class AppComponent {
 tasks: any = resData;   
 console.log(this.tasks); //While displaying just array
}

【问题讨论】:

  • 这能回答你的问题吗? How to filter object array based on attributes?
  • 您发布的 JSON 代码无效。具体来说,您在根数组中有一个带有重复键“Data”的对象。
  • @ErnestoStifano,我很抱歉。我的错。代码 UPD

标签: javascript json angular


【解决方案1】:

您可以为此使用map 函数。

let names = arr.map(element=> element.Data[0].name);

注意这里我假设 Data 只有一个实例。

let arr = [
      {
        "Data": [
          {
            "name": "Sony",
            "color": "black"
          }
        ]
      },
      {
        "Data": [
          {
            "name": "Apple",
            "color": "white"
          }
        ]
      },
      {
        "Data": [
          {
            "name": "Sony",
            "color": "red"
          }
        ]
      }
    ]
    
    let names = arr.map(element=> element.Data[0].name);
    console.log(names);

【讨论】:

    【解决方案2】:

    使用下面的代码

     let x = [
                {
                    "name": "Sony",
                    "color": "black"
                }, 
                {
                    "name": "Apple",
                    "color": "white"
                },   
                {
                    "name": "Sony",
                    "color": "red"
                },
                {
                  "name": "Sony",
                  "color": "black"
              }, 
              {
                  "name": "Apple",
                  "color": "white"
              }  
                  
        ];
        let output:any = [];
        let allNames = [];
        let uniqueNames = [];
    x.forEach(element => {
      let index = output.findIndex(status => status.name === element.name);
      if(index>=0){
        let obj = output[index];
        obj.count = obj.count+1
        output[index]= obj;
      }else{
        output.push({name:element.name,count:1});
        uniqueNames.push(element.name);
      }
    
      allNames.push(element.name);
    });
    console.log(allNames);
    console.log(uniqueNames);
    console.log(output);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 2021-10-11
      • 2020-01-18
      • 2015-01-12
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多