最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - convert string to Object in vue using Json.parse - Stack Overflow

programmeradmin5浏览0评论

I want to transform the the input value from the input element into an Object

using Json.parse() to convert the internData value does not convert the string to an Object

doing the same operation in the browser console returns the correct oute.

let data = "[1,2,3,4]";

JSON.parse(datas)
(4) [1, 2, 3, 4]

typeof z
'object'

What am I 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=".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=".5.17/vue.js"></script>
    
   
  </body>
</html>

I want to transform the the input value from the input element into an Object

using Json.parse() to convert the internData value does not convert the string to an Object

doing the same operation in the browser console returns the correct oute.

let data = "[1,2,3,4]";

JSON.parse(datas)
(4) [1, 2, 3, 4]

typeof z
'object'

What am I 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./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./ajax/libs/vue/2.5.17/vue.js"></script>
    
   
  </body>
</html>

Share Improve this question asked Sep 15, 2021 at 7:00 herbert mühlexherbert mühlex 2911 gold badge4 silver badges17 bronze badges 1
  • 1 transformData() { return JSON.parse(this.internData); }, – navnath Commented Sep 15, 2021 at 7:06
Add a ment  | 

2 Answers 2

Reset to default 3

The issue that you currently have is that you are not updating the internData from the transformData function youe should reassign internData with JSON parsed format of same inside the function just like below

transformData() {
  this.internData = JSON.parse(this.internData);
}

OR

You could make transformData as a puted property and watch for the type of transformData. Here you can get rid of the click event to parse the data.

new Vue({
  el: "#app",
  data() {
    return {
      datatype: "foo",
      internData: "[1, 2, 3]",
    };
  },
  methods: {
    whatFormat() {
      console.log(this.transformData);
      console.log(typeof this.transformData);
    }
  },
  puted: {
    transformData: function () {
      return JSON.parse(this.internData)
    }
  }
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <input v-model="internData" />
  <button type="button" @click="whatFormat">what data type</button>
</div>

Your transformData function transforms the string, but the newly created object is not assigned to the internData property.

This should do the trick:

transformData() {
  this.internData = JSON.parse(this.internData);
},
发布评论

评论列表(0)

  1. 暂无评论