Tengo este objeto en js:
var list = { "1": { "id": "1", "start_date": "2019-01-14", "end_date": "2019-01-14", "text": "some text one", "assistent": "12" }, "2": { "id": "2", "start_date": "2021-12-01", "end_date": "2021-12-01", "text": "another text", "assistent": "15" }, "3": { "id": "3", "start_date": "2021-12-02", "end_date": "2021-12-02", "text": "one more text", "assistent": "2" } }Quiero verificar si hay un "asistente" = 2 dentro de esta matriz de objetos y, en caso afirmativo, obtener un parámetro de "texto" para este asistente (tiene que ser "un texto más"). Lo que he probado:
var foundtext = list.find(x => x.assistent === '2').text; console.log(foundtext);(Mostrará que .find no es una función) También:
var foundtext = list.find(x => x.assistent == '2')['text']; console.log(foundtext);(mostrará el mismo error)
También probé esto:
for (var i=1; i <= list.length; i++) { console.log(list[i]); //and than perform the search inside list[i]; }y esto :
list.forEach(value => { console.log(value); //and than perform the search inside value; })Entonces, ¿cuál es el enfoque correcto para realizar este tipo de verificación?
Yo haría algo como esto.
const text = Object.values(list).find(entry => entry.assistent === "2")?.textEDITAR
Para la pregunta adicional en los comentarios a continuación.
const texts = Object.values(list) .filter(entry => entry.assistent === "2") .map(entry => entry.text)