search

Home  >  Q&A  >  body text

javascript - How to convert the data of the mini program calling interface into an array?

json:

{
    "success": 1,
    "data": [
        {
            "data": {
                "code": "1,2,3,4,5",
            },
            "type": "demo1",
        },
        {
            "data": {
                "code": "2,3,4,5,6",
            },
            "type": "demo2",
        }
    ]
}

JS code:

onLoad:function(){

var that = this;
wx.request({
  url: 'http://www.xxx.com/api.php?act=2', 
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    that.setData({
       codes:res.data.data[0].data.opencode.split(","),
     }),
  }
})

}

//As above, how can I turn the code in json into an array? I can't write the code in data[] to death. Otherwise, the previous view outputs a lot of duplicates, and I have been searching for a day but I still can’t find the answer.
Thank you guys in advance for giving me some ideas. I just learned small programs and I don’t understand many of them.

習慣沉默習慣沉默2827 days ago486

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:15:22

    Completed, after various attempts 2333

    html:

    <text class="numberli" wx:for="{{opencode[index]}}" wx:key="opencode">{{item}}</text>

    js:

    success: function(res) {
            var arrs = [];
            res.data.data.map(function(item){
              arrs.push(item.data.opencode.split(','))
              }),
            that.setData({
               opencode:arrs,
             }),
            console.log(arrs);
          }

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:15:22

    Is what you ultimately want to get a data structure like this:

    [
        {
            "data": {
                "code": ["1", "2", "3", "4", "5"]
            },
            "type": "demo1"
        },
        {
            "data": {
                "code": ["2", "3", "4", "5", "6"]
            },
            "type": "demo2"
        }
    ]

    If so, you can try the following code:

    that.setData({
        codes: res.data.map(item => {
            return {
                data: {
                    code: item.data.code.split(',')
                },
                type: item.type
            }
        })
    })

    reply
    0
  • Cancelreply