1

I want to know how to check if "novalue" exist For example:

{
    name: "maria",
    city_id: "novalue"
    ....
}

How i do this in Vue? Do it in <div v-if="condition"> or function?

sandrooco
  • 8,016
  • 9
  • 48
  • 86
  • just decode it with `JSON.parse` and test the existence of the value in the JS object – Kaddath Oct 12 '17 at 14:14
  • send me an example –  Oct 12 '17 at 14:15
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Oct 12 '17 at 14:16
  • its using Vue i can do it in if condition in the div –  Oct 12 '17 at 14:19

1 Answers1

8

In case you want to/can use ES7 features:

const containsKey = (obj, key ) => Object.keys(obj).includes(key);

Implementation example:

const someCoolObject = {name: 'Foo', lastName: 'Bar'};

const hasName = containsKey(someCoolObject, 'name');
const hasNickName = containsKey(someCoolObject, 'hasNickName');

console.log(hasName, hasNickName); // true, false

For Vue:

Component:

{
    methods: {
        containsKey(obj, key ) {
            return Object.keys(obj).includes(key);
        }
    },
    computed: {
        hasName() {
            return this.containsKey(this.someObject, 'name');
        }
    }
}

Template:

<div v-if="hasName"></div>
sandrooco
  • 8,016
  • 9
  • 48
  • 86