1

I have an array of 20 objects which I am getting from a database, each of them has an unique id. I also have an array of data with 2 ids. I want want to filter out only those 2 objects from the array of 20.

computed: {
    newHeros(){
      return this.getAllHeros.filter(newHero => {
        console.log(newHero.id);
        return newHero.id === this.heroForTab
      })
    }
  },


return {
   heroForTab: ['76NQjrYTdfbWN8xZOAvI', 'uDsm0BValBa31guJs10h']
};

1 Answer 1

1

User Array.filter to return what you need

var heroForTab = ['76NQjrYTdfbWN8xZOAvI', 'uDsm0BValBa31guJs10h'];
var arr = [{
    id: '76NQjrYTdfbWN8xZOAvI',
    name: 'aaa'
  },
  {
    id: '1111',
    name: 'bbb'
  },
  {
    id: '2222',
    name: 'ccc'
  },
  {
    id: 'uDsm0BValBa31guJs10h',
    name: 'ddd'
  }
]

var result = arr.filter(item => {
  return heroForTab.includes(item.id)
})

console.log(result)

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

Comments

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.