1

I need to display Div element if specific value exists on JSON object and I have tried using array I could success but I don't know how to do in JSON object. Simply I need to display div if value called "Channel" exists on JSON object.

Below code that I tried but it didn't work

activationTypes: [
{
"activationTypeId": 1,
"name": "SMS"
},
{
"activationTypeId": 2,
"name": "WEB"
},
{
"activationTypeId": 3,
"name": "Channel"
}
]

<div v-show="Object.values(activationTypes).includes('Channel')"> 
<p>test<test>
</div>
1
  • 1
    there is no "JSON object" ... it's just an array of objects you're dealing with, correcting that thought may help you actually try to solve your own problem ... Object.values(activationTypes) is an array of objects, you need to check if one of those objects has a name property with the value Channel Commented Aug 24, 2020 at 7:21

2 Answers 2

4

Simple Check:

activationTypes.some( item => item['name'] === 'Channel' );

Reference:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

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

2 Comments

you beat me to it by few secs :)
@nizantz you messed with the best
1

Use .find to search for the element by name:

new Vue({
     el:"#app",
     data(){
          return{
               activationTypes: [
                    {
                    "activationTypeId": 1,
                    "name": "SMS"
                    },
                    {
                    "activationTypeId": 2,
                    "name": "WEB"
                     },
                     {
                     "activationTypeId": 3,
                     "name": "Channel"
                      }
                ]
           }
     }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
     <div v-show="activationTypes.find(e=>e.name=='Channel')"> 
          <p>test</p>
     </div>
</div>

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.