-3

I receive answer from a form. I receive an array with objects where each object correspond to the answer of one question:

I want to access the answer.

First I was thinking to ask to use answers[i].t (for example), but the problem is that the number of the question depends of the answers submitted so I can't use index of the array. So I had the idea to access to the answer by using the id of the question.

For example: object.t where object.q === "64c3980b7e457155fed418db".

How can I do that in JavaScript?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
j.colin
  • 3
  • 1
  • 1
    The equivalent of a 'where' clause when searching through an array of objects would be either [`filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) or [`find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Rory McCrossan Aug 01 '23 at 10:07
  • Dupe: https://stackoverflow.com/a/13964186/519413 – Rory McCrossan Aug 01 '23 at 10:12

2 Answers2

0
const getId = answers?.findIndex(object.q === "64c3980b7e457155fed418db");
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Use the find method:

answers.find(item => item.q  === "64c3980b7e457155fed418db");