-1

I am currently stuck with a small problem.So, i have a JSON file. And i want to display only the data with London as city. How do i approach it. With my code, which is below , it will display all the data, which i don't want. I tried to do it with if statement, comparing the value, but it was not working

[
    {
        "id": 0,
        "city": "Budapest",
        "noDays": 3,
    },
    {
        "id": 1,
        "city": "London",
        "noDays": 5,
    },
    {
        "id": 2,
        "city": "London",
        "noDays": 2,
    },
    {
        "id": 3,
        "city": "Paris",
        "noDays": 3,
    },
]

function :

function City({ props }) {
  return (
    <div>
      {props.map((place) => (
        <p>{place.city}</p>
      ))}
    </div>
  );
}
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
Rambo
  • 1
  • 1
    props.filter(({city}) => city === 'London') – epascarello Feb 02 '22 at 17:38
  • There's no [JSON](https://www.json.org/json-en.html) in your example. `props` is an array of objects (otherwise you wouldn't be able to call `.map()`). Check the other methods the [`Array` prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) offers. – Andreas Feb 02 '22 at 17:39
  • [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting) – Andreas Feb 02 '22 at 17:41

1 Answers1

0

You could try something like this

  {props.map((place) => {
    if (place.city !== "London") return "";
    return <p>{place.city}</p>;
  })}
Mr_NAIF
  • 96
  • 6