javascript - How to convert the data of the mini program calling interface into an array?
習慣沉默
習慣沉默 2017-05-19 10:13:22
0
2
441

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.

習慣沉默
習慣沉默

reply all(2)
我想大声告诉你

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);
      }
为情所困

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
        }
    })
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template