-2

For example, we have the following array of objects

[
{ x_id: 3, name: abc, class: 3rd, subject: maths},
{ x_id: 33, name: sad, class: 4th, subject: maths},
{ x_id: 12, name: fds, class: 3rd, subject: phy},
{ x_id: 4, name: atgr, class: 10th, subject: sst},
]

Output for x_id 4

{ x_id: 4, name: atgr, class: 10th, subject: sst}
emma
  • 11
  • 3
  • 1
    If I understand the question well, I think your are looking for [find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) method. Ex: `arr.find(i => i.x_id === 4)` – Mina Sep 01 '22 at 20:59
  • `Array.find` is what you're looking for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find – jrend Sep 01 '22 at 21:00
  • 1
    Does this answer your question? [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) – pilchard Sep 01 '22 at 21:05

2 Answers2

0

You can do this with .find() for example this is your array

const array = [
{ x_id: 3, name: 'abc', class: '3rd', subject: 'maths'},
{ x_id: 33, name: 'sad', class: '4th', subject: 'maths'},
{ x_id: 12, name: 'fds', class: '3rd', subject: 'phy'},
{ x_id: 4, name: 'atgr', class: '10th', subject: 'sst'},
];

With this method you will iterate and find the element you are looking for within array

var result = array.find(x => x.x_id === 4);
console.log(result); //returns { x_id: 4, name: 'atgr', class: '10th', subject: 'sst'}

https://jsfiddle.net/kenpy/0uj2eo4f/3/

Chris G
  • 1,598
  • 1
  • 6
  • 18
0
    var array1 = [
    { x_id: 3, name: "abc", class: "3rd", subject: "maths" },
    { x_id: 33, name: "sad", class: "4th", subject: "maths" },
    { x_id: 12, name: "fds", class: "3rd", subject: "phy" },
    { x_id: 4, name: "atgr", class: "10th", subject: "sst" },
    { x_id: 4, name: "atgr", class: "12th", subject: "phy" }
]
// returns the first element, that satisfies the provided testing function.
let element = array1.find(element => element.x_id === 4);
console.log(element);

// returns the all elements, that satisfies the provided testing function.
let results = array1.filter(element => element.x_id === 4);
console.log(results);

find()

Use find() to search for the first Element, that satisfies the provided function ( element => element.x_id === 4 )

let element = array1.find(element => element.x_id === 4);
console.log(element);

returns

{ x_id: 4, name: 'atgr', class: '10th', subject: 'sst' }

filter()

Use filter() to search for all objects that satisfies the provided function ( element => element.x_id === 4 )

let results = array1.filter(element => element.x_id === 4);
console.log(results);

returns

[ 
  { x_id: 4, name: 'atgr', class: '10th', subject: 'sst' },
  { x_id: 4, name: 'atgr', class: '12th', subject: 'phy' } 
]
Bissy
  • 16
  • 3
  • 1
    Identical to the [10 year old duplicate](https://stackoverflow.com/a/35398031/13762301). Flag to close in these situations. – pilchard Sep 01 '22 at 21:39