Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React JS: Filter an array by a string

it's me again, now with reactjs.

I have a json, inside it have two "rows", they are Description and ubication. I need to filter the array by Description.

How can I filter this? The description it's in text format for example "Impact" or "Wiu wiu".

I know the function filter() of typescript have a numeric condition but no have a text condition or I haven't seen it been use for that.

Thank you so much for your help.

like image 880
Nicolas Papp Avatar asked Jul 22 '18 01:07

Nicolas Papp


1 Answers

No still you can do with Text as well,

let filtered = yourArray.filter(t=>t.Description ==='impact');

DEMO

let mydata = {
  "ArrayBotones": [
    {
      "descripcion": "Impacto",
      "ubicacion": "sonidos/impacto.mp3"
    },
    {
      "descripcion": "Soy Fede",
      "ubicacion": "sonidos/holasoyfede.wav"
    },
    {
      "descripcion": "Wiu wiu",
      "ubicacion": "sonidos/wiuwiu.wav"
    },
    {
      "descripcion": "3 carajos",
      "ubicacion": "sonidos/3carajos.wav"
    },
    {
      "descripcion": "Apurate Jose",
      "ubicacion": "sonidos/apuratejose.wav"
    },
    {
      "descripcion": "No, no se",
      "ubicacion": "sonidos/nonose.wav"
    }
  ]
};

let result = mydata.ArrayBotones.filter(t=>t.descripcion === 'Impacto');
console.log(result);
like image 135
Sajeetharan Avatar answered Oct 13 '22 00:10

Sajeetharan