My goal is to convert the input value from the input element into an Object
When I use JSON.parse()
to convert the internData
value, it doesn't turn the string into an Object as expected.
Interestingly, performing the same operation in the browser console gives me the correct outcome.
let data = "[1,2,3,4]";
JSON.parse(datas)
(4) [1, 2, 3, 4]
typeof z
'object'
I wonder what I might be missing here?
new Vue({
el: "#app",
data() {
return {
datatype: "foo",
internData: "[1, 2, 3]",
};
},
methods: {
transformData() {
//
//
JSON.parse(this.internData);
// this.internData.push(12);
return this.internData;
},
whatFormat(){
console.log(typeof this.internData)
}
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<html>
<body>
<div id="app">
<h5> {{ data }}</h5>
<input v-model="internData" @input="onInputme" />
<button type="button" @click="transformData">click to transform</button>
<button type="button" @click="whatFormat">what data type</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
</body>
</html>