0

I would like to obtain from All_reports the objects that include the key: comentarioAdmin. Now I'm obtaining in All_reports, all the reports, but I just want the reports that include the key comentarioAdmin. Thank you!

getReports() {
    this.Service.getReportes().subscribe((data) => {
      this.All_reports = data;

      console.log('respuesta de alumno->' + this.All_reports);
    });
  }

This is how I have my array of objects, some of them include comentarioAdmin and others don't:

[
.
.
.

{
        "_id": "5ee1b1f04e9bfe060050cacf",
        "nombre": "Mario",
        "apellido": "López No-Gattelll",
        "correo": "[email protected]",
        "direccion": "Varsovia 54, Juárez, 06600 Cuauhtemoc, CDMX",
        "referencia": "",
        "tipoPersona": "Otro",
        "comentario": "Hay una fuga a la mitad de la calle.",
        "numeroReporte": 2,
        "__v": 0,
        "comentarioAdmin": "PRUEBA"
    },
    {
        "_id": "5ee1c247634773d6e00e44d4",
        "nombre": "Fabián",
        "apellido": "Oropeza Oropeza",
        "correo": "[email protected]",
        "direccion": "Calle 10 de febrero 56 MH, CDMX",
        "referencia": "Es mi casa",
        "tipoPersona": "Otro",
        "comentario": "Justo en rente de mi casa hay una fuga y mi carro se moja cuando salgo.",
        "numeroReporte": 1,
        "__v": 0,
    },
.
.
.
]

1 Answer 1

1

You could do it with array filter() function. Try the following

var input = [{
    "comentario": "Hay una fuga a la mitad de la calle.",
    "numeroReporte": 2,
    "__v": 0,
    "comentarioAdmin": "PRUEBA"
  },
  {
    "referencia": "Es mi casa",
    "tipoPersona": "Otro",
    "comentario": "Justo en rente de mi casa hay una fuga y mi carro se moja cuando salgo.",
    "numeroReporte": 1,
    "__v": 0,
  }
];


var output = input.filter(item => {
  if (item.comentarioAdmin) {
    return item;
  }
});

console.log(output);

Typescript:

getReports() {
  this.Service.getReportes().subscribe((data) => {
    this.All_reports = data.filter(item => {
      if (item.comentarioAdmin) {
        return item;
      }
    });
    console.log('respuesta de alumno->' + this.All_reports);
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.