0

OK so this is what I am working with. I have 2 arrays The first array contains an ID number. That ID matches up to the second array minus the presence-chat. string.

Array 1:

Rooms: Array
 0: Object
 id: 1
 1: Object
 id: 2
 2: Object
 id: 3

Array 2:

roomUserCount: Object
 channels: Object
 presence-chat.1: Object
 user_count: 2
 presence-chat.2: Object
 user_count: 3
 presence-chat.3: Object
 user_count: 4

What I am trying to figure out is how would I write my v-for command to run through the whole thing. Whats happening before is another v-for that is saying

v-for="room in rooms" :key="room.id"

Now for the next step nested inside the first v-for I need to run another one to fill in the appropriate data.

the nested code would look like this but I need to fill it in.

<div v-for="" :key="">
 <div v-if="">
 {{ user_count }}
 </div>
 <div v-else="">
 0
 </div>
</div>

What do I do?

tony19
140k23 gold badges281 silver badges354 bronze badges
asked Oct 18, 2021 at 4:21

1 Answer 1

1

That's actually one array (rooms) that indexes an object (roomUserCount).

Lookup the object by room.id in roomUserCount.channels. If the object exists, render its user_count:

<template>
 <div v-for="room in rooms" :key="room.id">
 <div v-if="roomUserCount.channels['presence-chat.' + room.id]">
 {{ roomUserCount.channels['presence-chat.' + room.id].user_count }}
 </div>
 <div v-else>
 0
 </div>
 </div>
</template>

demo 1

Alternatively, you could simplify the template with optional chaining (?.) and nullish coalescing (??):

<template>
 <div v-for="room in rooms" :key="room.id">
 {{ roomUserCount.channels['presence-chat.' + room.id]?.user_count ?? 0 }}
 </div>
</template>

demo 2

answered Oct 18, 2021 at 5:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.