-2

I want to get a specific value from the array of the object using object Id.

array = [ 
         { f_name: a, l_name: q, id: 1},
         { f_name: b, l_name: w, id: 2},
         { f_name: c, l_name: e, id: 3},
        ]

How to get f_name value only using the id:1 ?..

Gowdham GM
  • 51
  • 1
  • 8
  • Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Jan 14 '21 at 10:25
  • Please search your title before asking – mplungjan Jan 14 '21 at 10:26

2 Answers2

1

You can use filter for this

  • filter returns all of the elements whichs elements id = 1 when the condition is element.id === 1 as a new array
  • then print out the name of this element

let array = [ 
         { "f_name": "a", "l_name": "q", "id": 1},
         { "f_name": "b", "l_name": "w", "id": 2},
         { "f_name": "c", "l_name": "e", "id": 3},
        ]
        
        
let result = array.filter((element) => element.id===1);
 console.log(result[0].f_name);
Aalexander
  • 4,987
  • 3
  • 11
  • 34
0

const array = [ 
 { f_name: a, l_name: q, id: 1},
 { f_name: b, l_name: w, id: 2},
 { f_name: c, l_name: e, id: 3},
];

array.filter(a => a.id === 1);//returns { f_name: a, l_name: q, id: 1}

More info on filter and array manipulation can be found here

groul
  • 119
  • 1
  • 9