D
D
Dolerum2019-12-07 12:05:52
JavaScript
Dolerum, 2019-12-07 12:05:52

How to access two identical objects?

Hello. An array comes from the server . It contains one or more objects. For example, there are two;
(2) [{…}, {…}]

0: {id: 489, name: "Nickname"}
1: {id: 345, name: "Email"}
length: 2
__proto__: Array(0)

The fact is that these objects have the same keys, but not values.
I can't get access to two objects at once and get their full values. For example, just dat.name does not work in the case below and throws an error name undefined. Tried to iterate like this.
<div>
<div>{data.map((dat, index) => {
    if(dat) {
for (let i=0; i< dat.length; i++) {
   return (
       <Link> {dat[i].name}</Link>
)
}}}
})}</div>

But [i] for some reason is always equal to 0 and only one is displayed - the first object.
What am I doing wrong? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tigran Abrahamyan, 2019-12-07
@Dolerum

Correct option.

let data = [
  {id: 489, name: "Nickname"},
  {id: 345, name: "Email"}
];

<div>
{
  data.map((dat, index) => {
    return (
      <Link>{dat.name}</Link>
    )
  })
}
</div>

In your case, the error is that you check dat.length instead of data.length in for and also dat[i].name instead of data[i].name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question