object数据

field[0]: "lb"
field[1]: "kh_id"
field[2]: "status"
type[0]: "select"
type[1]: "select"
type[2]: "test"
value[0]: "1"
value[1]: "55"
value[2]: "ok"

请教大神,上面这个数据怎样转换成下面这种?谢谢

filed = ["lb","kh_id","status"];
type = ["select","select","test"];
value = ["1","55","ok"];`
阅读 166
评论
    1 个回答
    • 11.7k

    如果数据有length属性,就可以使用slice、splice、reduce等直接获取
    Array.prototype.slice.call(a)

    否则就需要遍历对象,判断key值

    function objToArr(obj){
        let arr = []
        for(let k in obj){
            if(obj.hasOwnProperty(k) && parseInt(k) == k){
                arr[k] = obj[k]
            }
        }
    
        return arr
    }
    
    filed = objToArr(filed)
    type = objToArr(type)
    value = objToArr(value)
      撰写回答

      登录后参与交流、获取后续更新提醒