简体   繁体   中英

if else inside map function

I got an error:

Cannot read property 'map' of undefined

My JSON data:

{
    "data": [
        {
            "id": 1,
            "attention": 1
        },
        {
            "id": 2,
            "attention": 1
        },
        {
            "id": 3,
            "attention": 0
        }
    ]
}
_handleOutput() {
    var att = data.attention.map(function (data) {
      return data.attention
    });
}
console.log(att)

Is it possible to use if-else to compare the JSON output data to React DOM ? Something like this:

if (data.attention === true || "1") {
    return <span>&#9989;</span>;
} else {
    return <span>&#10060;</span>;
}

then i output into table list <td>{this._handleOutput()}</td>

let obj = {
    data: [
        {
            id: 1,
            attention: 1
        },
        {
            id: 2,
            attention: 2
        },
        {
            id: 3,
            attention: 0
        }
    ]
}

inside component define render function

render(){
return (
    <div>
     {obj.data.map(a=>{
      if(a.attention===1) 
       return <span>&#9989;</span>

       return <span>&#10060;</span>;  
     })}
    </div> 
  )
  }

data is array , not data.attention for this map is not working. map is working in the array.

You're trying to call map on a number field. It should be data.map instead.

Data is your array not data.attention. So use like this:

 {
        "data": [
            {
                "id": 1,
                "attention": 1
            },
            {
                "id": 2,
                "attention": 2
            },
            {
                "id": 3,
                "attention": 0
            }
        ]
    }

    var att = data.map(function (data) {
      return data.attention
    });
    console.log(att)
var data = [
    {
        "id": 1,
        "attention": 1
    },
    {
        "id": 2,
        "attention": 2
    },
    {
        "id": 3,
        "attention": 0
    }
]

var att = data.map(data => {
  console.log(data);

  if (data.attention === 1) {
    return '<span>&#9989;</span>';
  } else {
    return 'Not Fount';
  }
});

console.log(att);

Mistake in the tried solution

  1. data.attention is not an array.
  2. Check directly with value 1

Solution

  • Using Array.map to return a copy of jsx elements.
  • Using destructuring while looping through the array instead of writing item.attention
  • Checking the value so basically we can use ternary check (true? 1: 0), since the value of attention is either 1 or 0 which represents true or false we can use it in this way.

Notes to improve

 const jsonData = { "data": [{ "id": 1, "attention": 1 }, { "id": 2, "attention": 1 }, { "id": 3, "attention": 0 } ] } const App = () => { return ( <div> { jsonData.data.map(({attention}) => { return ( <div> { attention? <span>&#9989;</span>: <span>&#10060;</span> } </div> ) }) } </div> ) } ReactDOM.render(<App />, document.getElementById('root'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id='root'></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM