-1
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
    Please provide code of the actual data structure, not just some log output. Is it JSON, an object, or an array? – str Jun 18 '20 at 06:58
  • Object.keys(obj).map(key => obj[key]). Try to share more information, code or screenshot. – Hamada Jun 18 '20 at 07:06

3 Answers3

1
listOfNames = jsonValues.map(x=>x.name)
N1gthm4r3
  • 755
  • 1
  • 11
  • 23
1

You can try as follow.

 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);
HW Siew
  • 973
  • 8
  • 16
0

if it is json object like details = [{id:"",name:""},{id:"",name:""},{id:"",name:""}]

you can use map function like

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

to shorten this let names = values.map(value=>return value.name)

Rubyist
  • 6,486
  • 10
  • 51
  • 86