0

I have an object Like

enter image description here

How to get single row by Id?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Faisal
  • 584
  • 3
  • 11
  • 33
  • use index to get it – guradio Nov 28 '17 at 07:35
  • 2
    Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – JJJ Nov 28 '17 at 07:36

4 Answers4

3

You can use Array#find function and pass a condition into it like

arr.find(item => item.id === 1)

Example

const users = [
  {id: 1, name: 'A'},
  {id: 2, name: 'B'},
];

const user = users.find(item => item.id === 1);

console.log(user);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
2

Use find()

var item = yourArray.find(item => item.id === 2053);

DEMO

const yourArray = [
  {Id: 2053, title: 'sass'},
  {Id: 2054, title: 'sdss'},
];
const found = yourArray.find(item => item.Id ===2053);
console.log(found);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
2

Yo can also use var requiredItem = array.filter(i => i.id == 2054)

Example

var arrayNew = [
  {id: 2053, name: 'sxsxs'},
  {id: 2054, name: 'sss'}  
];

var requiredItem = arrayNew.filter(i => i.id == 2054);

console.log(requiredItem);
Nikhil Ghuse
  • 1,258
  • 14
  • 31
1

let obj = [{
 "id" : 1,
 "Title" : "Hi"
},{
"id" : 11,
 "Title" : "Hello"
}]

function filterById(ids) {
  return  obj.filter((obj) => {return obj.id == ids})  
}

console.log(filterById(11))
Rajkumar Somasundaram
  • 1,225
  • 2
  • 10
  • 20