美文网首页
json转数组

json转数组

作者: 白麝香 | 来源:发表于2018-05-02 17:54 被阅读0次

    首先 对象有一个很好的方法:Object.keys(),该方法返回一个数组。例如:

    这里可以看https://blog.csdn.net/u014035151/article/details/53135610,

    懒得粘贴了。

    用这个方法,我们可以遍历json对象 设key和value值了

    const list = Object.keys(json).map( (item,indx) => ({key:item, value:json[item]}) )

    这时候 list就是一个 数组对象了。

    比如:

    var jsonA={"企业运营":{"2018-05-01":[3,2,7],"2018-04-30":[1,1,8],"2018-04-29":[2,1,8],"2018-04-28":[1,3,8],"2018-04-27":[0,4,9],"2018-04-26":[0,4,9],"2018-04-25":[0,5,8],"阿里内外":{"2018-05-01":[1,0,6],"2018-04-30":[1,0,6],"2018-04-29":[0,1,6],"2018-04-28":[1,1,7],"2018-04-27":[1,1,6],"2018-04-26":[1,1,7],"2018-04-25":[2,1,6],"2018-04-24":[0,1,8],"信息技术":{"2018-05-01":[0,1,2],"2018-04-30":[0,1,1],"2018-04-29":[0,1,1],"2018-04-28":[0,0,3],"2018-04-27":[0,0,3],"2018-04-26":[0,1,1],"2018-04-25":[0,0,2],"2018-04-24":[0,1,1],"智慧园区":{"2018-05-01":[0,0,9],"2018-04-30":[0,1,5],"2018-04-29":[0,0,3],"2018-04-28":[0,1,7],"2018-04-27":[0,0,6],"2018-04-26":[0,0,8],"2018-04-25":[0,0,9],"2018-04-24":[0,0,7],"AECP与中台":{"2018-05-01":[0,0,4],"2018-04-30":[0,0,3],"2018-04-29":[0,0,4],"2018-04-28":[0,0,6],"2018-04-27":[0,0,6],"2018-04-26":[0,0,5],"2018-04-25":[0,0,5],"2018-04-24":[0,0,5];

    console.log(Object.keys(jsonA))  //object

    var arryN = Object.keys(jsonA).map((item, ind)=>({

          key: item,

          value: Object.keys(jsonA[item]).map((ite,ind)=>({key: ite,value: jsonA[item][ite]})),

      }));

    console.log(typeof(arryN))

    console.log(arryN)

    有兴趣的可以拷这段代码去试试,因为用了es6,记得引一下babel。顺道贴个地址 https://unpkg.com/babel-standalone@6/babel.min.js

    相关文章

      网友评论

          本文标题:json转数组

          本文链接:https://www.haomeiwen.com/subject/pmicrftx.html