-1

Here i m given some data in Json formate. I want to fillter it with it atrribute .

[
{
    "_id": "1",
    "name": "ShalimarBhagh",
    "city_id": "1",
    "location_id": "1",
    "country_name": "India"
},
{
    "_id": "2",
    "name": "Janpat, Delhi",
    "city_id": "1",
    "location_id": "2",
    "country_name": "India"
},
{
    "_id": "3",
    "name": "MSP, Delhi",
    "city_id": "1",
    "location_id": "3",
    "country_name": "India"
},
{
    "_id": "4",
    "name": "MSP, Pune",
    "city_id": "2",
    "location_id": "4",
    "country_name": "India"
},
{
    "_id": "5",
    "name": "Anand Pur",
    "city_id": "1",
    "location_id": "5",
    "country_name": "India"
},

]

From the Above Json file just want to display only those object which has arrtibute with that value ( name:"Delhi")

it Should be Displayed like that by using node.js and React

{
    "_id": "2",
    "name": "Janpat, Delhi",
    "city_id": "1",
    "location_id": "2",
    "country_name": "India"
},
{
    "_id": "3",
    "name": "MSP, Delhi",
    "city_id": "1",
    "location_id": "3",
    "country_name": "India"
},
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Lakshya Thakur Oct 02 '22 at 16:03

3 Answers3

0

Just use array's filter method:

const data = [
  {
    _id: "1",
    name: "ShalimarBhagh",
    city_id: "1",
    location_id: "1",
    country_name: "India",
  },
  {
    _id: "2",
    name: "Janpat, Delhi",
    city_id: "1",
    location_id: "2",
    country_name: "India",
  },
  {
    _id: "3",
    name: "MSP, Delhi",
    city_id: "1",
    location_id: "3",
    country_name: "India",
  },
];

const filtered = data.filter((item) =>
  item.name.toLowerCase().includes("delhi")
);

console.log(filtered);
Mauro Aguilar
  • 1,093
  • 13
  • 22
  • Hi, @MauroAguilar thank you very much bro it helps me a lot, and I want to practice more with JSON so please refer me best website if you know. – Suman kumar Oct 02 '22 at 16:20
0

Let's say your array is stored in a variable called elements.

What you can do is filter all of the elements with a condition that the name includes string "Delhi"

const elements = [
{
    "_id": "1",
    "name": "ShalimarBhagh",
    "city_id": "1",
    "location_id": "1",
    "country_name": "India"
},
{
    "_id": "2",
    "name": "Janpat, Delhi",
    "city_id": "1",
    "location_id": "2",
    "country_name": "India"
},
{
    "_id": "3",
    "name": "MSP, Delhi",
    "city_id": "1",
    "location_id": "3",
    "country_name": "India"
},
{
    "_id": "4",
    "name": "MSP, Pune",
    "city_id": "2",
    "location_id": "4",
    "country_name": "India"
},
{
    "_id": "5",
    "name": "Anand Pur",
    "city_id": "1",
    "location_id": "5",
    "country_name": "India"
}];

const result = elements.filter(element => element.name.includes("Delhi"));

console.log(result);

Now result contains all elements whose name includes Delhi. Note that this does not modify the original array, instead it returns a new array.

milansav
  • 26
  • 1
  • 4
  • Hi, @milansav thank you very much bro it helps me a lot, and I want to practice more with JSON so please refer me best website if you know. – Suman kumar Oct 02 '22 at 16:19
-1

You can use the .filter() function. If the array is named arr then:

var new_array = arr.filter(function callback(element, index, array) {
    return (element.name.includes("Delhi"))
});

Where new_array is the filtered array.

node_modules
  • 4,790
  • 6
  • 21
  • 37
Sharjeel Ahmed
  • 2,051
  • 2
  • 17
  • 25
  • 1
    Hi, @SharjeelAhmed thank you very much bro it helps me a lot, and I want to practice more with JSON so please refer me best website if you know. – Suman kumar Oct 02 '22 at 16:20