0

I have an array of objects,I would like to display data based on some condition.If the user id matches with data id display name,age for that id.Also is it possible create single divs and display multiple div by looping instead of defining it 3 times for 3 different id.

              <div class="content" v-for="person in persons>
              <div v-for="user in users"
              :key="user.id">
                  <div v-if="user.seqId == person.id" >
                   {{person.name}}          
                  </div>
                <div v-if="user.seqId == person.id">
                   {{person.name}} 
                  </div>
                </div>
                <div v-if="user.seqId == person.id">
                   {{person.name}}  
                  </div>
                </div>
                </div>

            const persons= [
                {
                   name: 'Adam',
                   age:20,
                   id: 1,
                },
                   {
                   name: 'Paul',
                    age:20,
                    id: 2,
                },
                {
                  name: 'James',
                  age:20,
                  id: 3,
                 },
                 ]
              data() {
                return {
                  persons,
                  };
                  }
2
  • really hard to know what you are trying to achieve Commented Nov 21, 2019 at 0:51
  • if user.seqId equal to 1 then frist item from array should display.Name should be Adam .If the user.seqId is 2 ,2nd item from array should display.Name should be Paul .user.seqId should match id from Data section and display name according to that Commented Nov 21, 2019 at 1:15

1 Answer 1

2

Create a computed property to merge the matches into a single array..

computed: {
    matchingUsers() {
        // find persons that exist in users by
        // matching p.id == u.seqId
        return persons.filter(p=>{
            return users.find(u=>u.seqId===p.id)
        })
    }
}

Then iterate the single array...

<div class="content" v-for="(person,i) in matchingUsers" :key="i">
    {{person.name}} - {{person.age}}
</div>

Codeply demo

Sign up to request clarification or add additional context in comments.

2 Comments

I have one more question.What about if the Person is defined as Prop and getting data from another vue.
Will you please mark this answer as accepted? do you mean Persons (not Person) is via prop?

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.