1

I'm trying to map some data that I have stored in an array (the screenshot below), but I can't seem to access them. I get two cards (which is right since there are two arrays in the array), but they are nothing is writen in them. Does anyone know why?

{this.state.serials.map((number) => (
            <Card>
              <Card.Body>{number.s.l}, {number.s.ta}</Card.Body>
            </Card>
          ))}

Screenshot

Thanks in advance!

*Edit: This is what I get if I console.log(number.s):

enter image description here

2
  • 1
    Try this: <Card.Body>{number.s[0].l}, {number.s[0].ta}</Card.Body> Commented Nov 20, 2020 at 8:23
  • When I try that I get an error - cannot read property 'l' of undefined. Commented Nov 20, 2020 at 8:27

2 Answers 2

1

number.s is an array

{this.state.serials.map((number) => {
    return number.s && number.s.length? (<Card>
      <Card.Body>{number.s[0].l}, {number.s[0].ta}</Card.Body>
    </Card>
  ) : null
})}
Sign up to request clarification or add additional context in comments.

5 Comments

When I try that I get an error - cannot read property 'l' of undefined.
make sure that number.s has elements with ternery. i will edit the post in second
if its not working, try to console.log( number.s) and post it here
I edited the question and added the console.log(number.s)
have you tried my solution and it didn't work? try also console.log(number.s[0])
1

The structure of the state serials is like bellow

let data = [
  { 
    s: [
      {l: -127, ta: "878", d: [1]}
    ]
  },
  { 
    s: [
      {l: 22.5625, ta: "003", d: [1]}
    ]
  },
];

console.log(data);

So to access value which are in key l for each item you must perform it like this

{this.state.serials.map((number) => (
    <Card>
        <Card.Body>{ (number.s[0]).l }, {(number.s[0]).ta}</Card.Body>
    </Card>
))}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      serials: [
        { 
          s: [
            {l: -127, ta: "878", d: [1]}
          ]
        },
        { 
          s: [
            {l: 22.5625, ta: "003", d: [1]}
          ]
        },
      ]
    }
  }
  
  render() {
    return this.state.serials.map((number) => (
      <p>
        <span><strong>{ (number.s[0]).l }</strong>, { (number.s[0]).ta }</span>
      </p>
    ));
   }
}


ReactDOM.render(<App />, document.querySelector("#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>

1 Comment

Unfortunately, when I tried that I got an error - cannot read property 'l' of undefined, as in other cases.

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.