简体   繁体   中英

How to conditionally output data based on JavaScript object?

Any help? Let's say i have this array of objects.

const data = {
post: [
  {
  id: 1,
  name: "lorem A",
  lastseen: 1,
  },
  {
  id: 2,
  name: "lorem B",
  lastseen: 16,
  },
  {
  id: 3,
  name: "lorem C",
  lastseen: 3,
  },
  {
  id: 4,
  name: "lorem D",
  lastseen: 10,
  },
]`}

I'm working with simple react time component to display "hr" or "hrs". How to check if "data.lastseen" value is greater than 1? So i can display like this "1 hr" or "2 hrs"

I know to map this but conditionally add and display "hr" or "hrs" to each object I'm having trouble with.

const mapData = data.map((seen) => seen.lastseen)

I googled it to extract values from Object we can use "Object.values()" But not sure how to combine with map.

Updated code This is my react component

<Main>
{Request.data.post.map((user) => {
    return (
      <Col
      //I'm display other component here 
        <User user={user} />
      //I'm display other component here 
       </Col>
    );
})}

 const User = ({ user }) => {
  return (
    <Col>
      <Seen user={user} />
    </Col>
  );
};


const Seen = ({ user }) => {
  return <React.Fragment> {user.map((seen) =>
        seen.lastseen == 1 ? seen.lastseen + "hr" : seen.lastseen + "hrs"
      )}</React.Fragment>;
};

由于您无法在jsx中使用if if else,因此请使用如下三元运算符:

const mapData = data.map(seen => (seen.lastseen == 1 ? 'hr': 'hrs'));
for(var i=0; i<data.length; i++) {
    if(data[i].lastseen === 1) {
        // display 1 + 'hr'
    } else if(data[i].lastseen > 1) {
        //diplay data[i].lastseen + 'hrs'
    }
}

here is my solution:

 import React from "react"; class App extends React.Component { render() { const data = [ { id: 1, name: "lorem A", lastseen: 1 }, { id: 2, name: "lorem B", lastseen: 16 }, { id: 3, name: "lorem C", lastseen: 3 }, { id: 4, name: "lorem D", lastseen: 10 } ]; return ( <div> {data.map(item => { return item.lastseen === 1 ? ( <p key={item.id}>{item.lastseen}hr</p> ) : ( <p key={item.id}>{item.lastseen}hrs</p> ); })} </div> ); } } export default App; 

Now, i figured it out thanks for help.

I updated my seen component to

const seen = ({ user }) => {
  return (
    <React.Fragment>
      {user.lastseen == 1 ? user.lastseen + "hr" : user.lastseen + "hrs"}
    </React.Fragment>
  );
};

My mistake I thought "user" is still an array which is not it's an object. That's why I keep getting an error "TypeError: user.map is not a function"

From your code above

const Seen = ({ user }) => {
  return <React.Fragment> {user.map((seen) =>
        seen.lastseen == 1 ? seen.lastseen + "hr" : seen.lastseen + "hrs"
      )}</React.Fragment>;
};

will thrown an error

TypeError: user.map is not a function

As user is an object you can use Object.keys and map over it. So change your code

const Seen = ({ user }) => {
     return <React.Fragment> { Object.keys(user).map((key) => (key == 'lastseen' ? (user[key] == 1 ? user[key] + ' hr': user[key] +' hrs') : null)) } </React.Fragment>;
 };

and it will work fine.

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