Nitish Kumar
Nitish Kumar

Reputation: 6276

How to find elements in an array in vuejs or Javascript

I'm having an array of elements in vuejs, I've some data manipulation with them Like I've a select dropdown which shows the company information and each company information has a tag. Tags are of one sub level, I've joined both the tags as one and stored in database, whenever I select the parent company I want these tags to be selected, I mean it should have class btn-warning and rest all to be btn-primary don't be confused with other select, there options are coming from the filter of first select

So suppose my tag data value is: Investor-Mutual fund, it should split up and Investor button should be selected and Mutual Fund button should have class of btn-primary and this should be changeable only with the select list only

Here's my code pen: https://codepen.io/anon/pen/eWXPaK?editors=1010

This will give you idea what I'm trying to do. Guide me

Upvotes: 3

Views: 20401

Answers (1)

Bert
Bert

Reputation: 82489

I believe this is what you want.

if (!this.selectedCompanies.length > 0){
  const str = this.model.data.find(d => d.name === newValue.label)
  const [currentTag, selectedTag] = str.tag.split("-")
  this.currentTag =  currentTag
  this.selectedTag =  this.tags[this.currentTag].find(t => t.display === selectedTag)
}

Updated pen.

Upvotes: 5

Related Questions