遍历json对象以获取特定键的值

时间:2020-06-18 06:56:18

标签: javascript json angular

0: {id: "7B5B201E-35AA-48A1-B919-002445319F8B", name: "Naman Sabarwal"}

1: {id: "EA6672BA-4F7A-4214-A37F-00716CE698C9", name: "me name"}

2: {id: "01F29920-9206-42DF-8151-00A6A080C501", name: "Nitesh Negi"}

I want to get a list such that the list contains only the name key values.

listOfNames = ['sonu singh','me name','harman jain']


How to get all the values of the key 'name'?

3 个答案:

答案 0 :(得分:1)

listOfNames = jsonValues.map(x=>x.name)

答案 1 :(得分:1)

您可以尝试以下操作。

 let data = [
        {id: "7B5B201E-35AA-48A1-B919-002445319F8B", name: "Naman Sabarwal"},
        {id: "EA6672BA-4F7A-4214-A37F-00716CE698C9", name: "me name"},
        {id: "01F29920-9206-42DF-8151-00A6A080C501", name: "Nitesh Negi"}
    ]; // assume the data is in array 

let result = data.map( d => d.name );

console.log(result);

答案 2 :(得分:0)

如果是json对象,例如 详细信息= [{{id:“”,name:“”},{id:“”,name:“”},{id:“”,name:“”}]]

您可以使用地图功能

function getnames(){
let names = []
details.map((detail)=>{
names.push({name:detail.name})
return names
}

以缩短此时间 让名称= values.map(value =>返回value.name)