How to remove an array from within another array by filtering those that have the X value in a specific key in Nodejs?

Asked

Viewed 55 times

-1

I’m starting at Node.js and have a little difficulty dealing with array/json, I’m in the learning process yet.

Assuming the following array of arrays:

Base array that should be filtered:

[
   {
      "id": 1,
      "nome":"Jonh Doe",
      "city":"NY",
      "status":"Single"
   },
   {
      "id": 2,
      "nome":"Mary",
      "city":"NY",
      "status":"Single"
   },
   {
      "id": 3,
      "nome":"Tom",
      "city":"CO",
      "status":"Married"
   },
   {
      "id": 4,
      "nome":"Mark",
      "city":"MI",
      "status":"Divorced"
   },
   {
      "id": 5,
      "nome":"Carl",
      "city":"NY",
      "status":"Single"
   },
]

You would need to save this same array in a variable but only where the key city is equal to NY. I researched about .map, .filter, .reduce, .indexOf but I could not reach a result. How could I achieve the following result:

Filtered array from NY only:

[
   {
      "id": 1,
      "nome":"Jonh Doe",
      "city":"NY",
      "status":"Single"
   },
   {
      "id": 2,
      "nome":"Mary",
      "city":"NY",
      "status":"Single"
   },
   {
      "id": 5,
      "nome":"Carl",
      "city":"NY",
      "status":"Single"
   },
]

2 answers

0


Hey, I don’t think you did your research, because filter and map can do this in a very simple way:

var array = [
   {
      "id": 1,
      "nome":"Jonh Doe",
      "city":"NY",
      "status":"Single"
   },
   {
      "id": 2,
      "nome":"Mary",
      "city":"NY",
      "status":"Single"
   },
   {
      "id": 3,
      "nome":"Tom",
      "city":"CO",
      "status":"Married"
   },
   {
      "id": 4,
      "nome":"Mark",
      "city":"MI",
      "status":"Divorced"
   },
   {
      "id": 5,
      "nome":"Carl",
      "city":"NY",
      "status":"Single"
   },
];

var novoArray = array.filter(x => x.city == "NY");
console.log(novoArray);

In this example I used filter, that must receive a Function or expression that evaluates each element of the array, and returns true/false for each element, and the elements where the condition is true are returned. That is, the elements where city == "NY" are returned, returning a new array only with these elements.

  • Well explained Ricardo. The examples I saw were always var array = [1, 2, 3, 4, 5] and filtering a specific number or something, I thought I would have different large ones to remove the entire array based on a key. I thought the .filter would remove only that key and not the entire array. Thank you.

0

Actually it’s not just Node.js it’s Javascript in general. As you mentioned, you can sort it out in a row with filter, assuming your initial array is called cities:

cidades.filter(i => i.city === "NY")

More detailed version:

cidades.filter(cidadeIteracao => {
  if (cidadeIteracao.city === "NY") {
    return cidadeIteracao;
  }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.