javascript 循环遍历JSON对象以获取特定键[duplicate]的值

cld4siwp  于 2023-05-05  发布在  Java
关注(0)|答案(3)|浏览(117)

此问题已在此处有答案

From an array of objects, extract value of a property as array(25个回答)
两年前关闭。

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'?
csga3l58

csga3l581#

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

wztqucjr2#

你可以尝试如下。

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);
tgabmvqs

tgabmvqs3#

如果是json对象,如details = [{id:“",name:“"},{id:“",name:“"},{id:“",name:“"}]
你可以使用map函数

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

要缩短此代码,请输入names =values.map(value=〉returnvalue.name)

相关问题