0

How can I add an if statement or filter out null and empty values in my code? I need to remove/ not show the name value that is null or empty.

componentDidMount() {
    fetch('https://url')
        .then(res => res.json())
        .then((data) => {
            data.sort(function (y,x){
                return y.listId - x.listId;
          })
          data.sort();
          this.setState({ items: data })
          console.log(this.state.items)
     })
     .catch(console.table)
  }

  return (
      <div className="list-container">
          <h1>My List</h1>
          {this.state.items.map((item) => (
              <div className="list">
          )}

Here's an example of my json array:

[
    {"id": 755, "listId": 2, "name": ""},
    {"id": 203, "listId": 2, "name": ""},
    {"id": 684, "listId": 1, "name": "Item 684"},
    {"id": 276, "listId": 1, "name": "Item 276"},
    {"id": 736, "listId": 3, "name": null},
    {"id": 926, "listId": 4, "name": null}
]

3 Answers 3

1

You can use the filter array method in this way:

data.filter(d => d !== "" && d !== null)

Add this line before the sorting function like this:

componentDidMount() {
    fetch('https://url')
    .then(res => res.json())
    .then((data) => {
      const filteredData = data.filter(d => d !== "" && d !== null)
      filteredData.sort(function (y,x){
        return y.listId - x.listId;
      })
      filteredData.sort();
      this.setState({ items: filteredData })
      console.log(this.state.items)
    })
    .catch(console.table)
}

Also (probably unrelated), why are you sorting two times?

Sign up to request clarification or add additional context in comments.

Comments

0

let data = [
{"id": 755, "listId": 2, "name": ""},
{"id": 203, "listId": 2, "name": ""},
{"id": 684, "listId": 1, "name": "Item 684"},
{"id": 276, "listId": 1, "name": "Item 276"},
{"id": 736, "listId": 3, "name": null},
{"id": 926, "listId": 4, "name": null}] 

console.log(data.filter(({name}) => name))

Comments

0

You can filter the array by doing this:

const items = this.state.items.filter(item => item.name !== '' && item.name !== null);

This will return a new array that doesn't have items with a blank or null name.

Edit for comment:

You can add this in your code like this:

componentDidMount() {
    fetch('https://url')
    .then(res => res.json())
    .then((data) => {
      const filteredData = data.filter(item => item.name !== '' && item.name !== null);
      filteredData.sort(function (y,x){
        return y.listId - x.listId;
      })
      filteredData.sort();
      this.setState({ items: filteredData })
      console.log(this.state.items)
    })
    .catch(console.table)
}

4 Comments

Where in my code can I add the suggested line of code?
You can add that before setting the state like @christos stated in their answer. I will edit my answer
Thank you for the edit. I tried the suggested code and it gives me back an empty array @pbrune
Sorry about that. I was using this.state.items instead of data. I have updated the code snippet in my original answer. Let me know if that works for you

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.