How to find elements in an array in vuejs or Javascript
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.
Related videos on Youtube
Nitish Kumar
Beginner to the code world. Curios to know lot more things
Updated on August 22, 2022Comments
-
Nitish Kumar over 1 year
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 classbtn-warning
and rest all to bebtn-primary
don't be confused with other select, there options are coming from the filter of first selectSo suppose my tag data value is:
Investor-Mutual fund
, it should split up andInvestor
button should be selected andMutual Fund
button should have class ofbtn-primary
and this should be changeable only with the select list onlyHere'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
-
Nitish Kumar almost 7 yearsError in callback for watcher "company_name": "TypeError: Cannot read property 'split' of undefined". But I want exactly as you did in codepen :(
-
Bert almost 7 years@NitishKumar I don't get an error in the pen. What is the difference?
-
Nitish Kumar almost 7 yearsOk solved. Was getting
model.data.type
and was writingmodel.data.tag
-
Nitish Kumar almost 7 yearsCan I put @click.prevent? in
v-for="tag in tags[currentTag]"
, as while clicking the buttons it is redirecting -
Bert almost 7 years@NitishKumar Not sure how to answer. There doesn't appear to be any reason a re-direct would happen in the pen.