whggwu
2018-04-16 06:22
采纳率: 66.7%
浏览 965

js数组循环创建问题求解

已知以下数组:
arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];
brr=['date','type','val'];
如何构建以下数组:
crr=[{'date':'2018-01-01','type':'aaa','val':12.11},{...},{...},{...}];
菜鸟,对数组理解不深,可否分别使用JavaScript、jQuery两种方式演示;

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

10条回答 默认 最新

  • 求帮助谁来帮我 2018-04-16 08:12
    已采纳
     var arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];
            var brr=['date','type','val'];
            function js(){
                var crr = new Array();
                for (var i = 0; i < arr.length;  i++){
                    var a = new Array();
                    a[brr[0]]= arr[i][0];
                    a[brr[1]]= arr[i][1];
                    a[brr[2]]= arr[i][2];
                    crr.push(a);
                }
            }
            function jq(){
                var crr = new Array();
                $.each(arr, function(i, e){
                    var a = new Array();
                    a[brr[0]]= e[0];
                    a[brr[1]]= e[1];
                    a[brr[2]]= e[2];
                    crr.push(a);
                });
            }
            js();
            jq();
    
    已采纳该答案
    打赏 评论
  • z17772505 2018-04-16 06:39

    只需要根据arr遍历就可以了,brr里面的内容可以写到循环内

    打赏 评论
  • poi_eyes 2018-04-16 06:41

    var len = arr.length;
    var crr=new Array()
    for(var i= 0; i < len; i++)
    {
    var tempArr = arr[i];
    var obj = {'date':tempArr[0],'type':tempArr[1],'val':tempArr[2]}
    crr[i] = obj;
    }

    打赏 评论
  • a718089112 2018-04-16 06:42

    参考下:

     var crr = new Array();
    for(int i=0;i<3){
     crr[i] = "{'"+brr[0]+"':'"+arr[i][0]+"','"+brr[1]+"':'"+arr[i][1]+"','"+brr[2]+"':'"+arr[i][2]+"',}";
    }
    
    打赏 评论
  • 
        arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];
        brr=['date','type','val'];
        var crr = []
        for (var i = 0; i < arr.length; i++) {
            var o = {}
            for (var j = 0; j < arr[i].length; j++) {
                if (brr[j]) o[brr[j]] = arr[i][j]
            }
            crr.push(o)
        }
        alert(JSON.stringify(crr))
    
    打赏 评论
  • androidTV4 2018-04-16 07:12

    var crr = new Array();
    for(int i=0;i<3){
    crr[i] = "{'"+brr[0]+"':'"+arr[i][0]+"','"+brr[1]+"':'"+arr[i][1]+"','"+brr[2]+"':'"+arr[i][2]+"',}";
    }

    打赏 评论
  • 求帮助谁来帮我 2018-04-16 08:11

    var arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];
    var brr=['date','type','val'];
    function js(){
    var crr = new Array();
    for (var i = 0; i < arr.length; i++){
    var a = new Array();
    a[brr[0]]= arr[i][0];
    a[brr[1]]= arr[i][1];
    a[brr[2]]= arr[i][2];
    crr.push(a);
    }
    }
    function jq(){
    var crr = new Array();
    $.each(arr, function(i, e){
    var a = new Array();
    a[brr[0]]= e[0];
    a[brr[1]]= e[1];
    a[brr[2]]= e[2];
    crr.push(a);
    });
    }
    js();
    jq();

    打赏 评论
  • 求帮助谁来帮我 2018-04-16 08:12

    var arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];
    var brr=['date','type','val'];
    function js(){
    var crr = new Array();
    for (var i = 0; i < arr.length; i++){
    var a = new Array();
    a[brr[0]]= arr[i][0];
    a[brr[1]]= arr[i][1];
    a[brr[2]]= arr[i][2];
    crr.push(a);
    }
    }
    function jq(){
    var crr = new Array();
    $.each(arr, function(i, e){
    var a = new Array();
    a[brr[0]]= e[0];
    a[brr[1]]= e[1];
    a[brr[2]]= e[2];
    crr.push(a);
    });
    }
    js();
    jq();

    
    
    打赏 评论
  • lihongbiao0610 2018-04-16 08:18

    arr=[['2018-01-01','aaa',12.11],['2018-02-01','bbb',12.99],['2018-09-01','aaa',1.11],['2018-01-01','ccc',152.11]];
    brr=['date','type','val'];

    var crr=[];
    for(var i=0;i<arr.length;i++){
    var aa=arr[i];
    var obj={};
    for(var j=0;j<aa.length;j++){
    var key=crr[j];
    var value=aa[j];
    obj[key]=value;
    }
    crr.push(obj);
    }

    打赏 评论
  • carrykingdow 2018-04-17 05:03
           let arr = [
                ['2018-01-01', 'aaa', 12.11],
                ['2018-02-01', 'bbb', 12.99],
                ['2018-09-01', 'aaa', 1.11],
                ['2018-01-01', 'ccc', 152.11]
            ];
            let brr = ['date', 'type', 'val'];
            let crr = [];
            for (let item of arr) {
                let obj = {};
                obj[brr[0]] = item[0];
                obj[brr[1]] = item[1];
                obj[brr[2]] = item[2];
                crr.push(obj);
            }
            console.log('crr', crr);
    
    打赏 评论

相关推荐 更多相似问题