转换对象的多维数组对单维阵列的对象的Javascript [英] Convert a multi-dimensional array of objects to single-dimensional array of objects Javascript

查看:102
本文介绍了转换对象的多维数组对单维阵列的对象的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正在给角的NG-重复破坏对象的多维数组,所以我想将其转换为一维数组。我尝试和失败这样做使用的forEach 。当我尝试使用的console.log(newData)运行功能之后,它会返回一个空数组。我是不是使它太简单了,还是我做一个愚蠢的错误?

\r
\r

//的样本数据(ALLDATA):\r
\r
变种ALLDATA = [{\r
  期间:4,\r
  uploadDate:2015-11-19T21:00:00.000Z,\r
  部分: [{\r
    转移code:8675309,\r
    细节:[{\r
      充值卡号码:[34,22]\r
      供应商:吉米,\r
      说明:嗒嗒\r
      量注:T 45,555.00\r
    }]\r
  }]\r
},{\r
  期间:4,\r
  uploadDate:2015-11-19T21:00:00.000Z,\r
  部分: [{\r
    转移code:45576543,\r
    细节:[{\r
      充值卡号码:[22,33],\r
      供应商:琼森\r
      说明:陷阱乐,\r
      量注:T 12,345.00\r
    }]\r
  }]\r
}]\r
\r
// code推向新的数组:\r
\r
变种newData = [];\r
\r
功能newArray(){\r
  allData.forEach(函数(顶部){\r
    top.section.forEach(函数(MID){\r
      mid.details.forEach(功能(低){\r
        newData.push({\r
          供应商:low.vendor,\r
          充值卡号码:low.voucherNumber,\r
          说明:low.description,\r
          量:low.amount,\r
          转让code:mid.transfer code,\r
          uploadDate:top.uploadDate,\r
          时间:top.period\r
        })\r
      })\r
    })\r
  })\r
  返回newData;\r
}\r
newArray();\r
document.body.textContent = JSON.stringify(newData);

\r

\r
\r


解决方案

我不知道,那你的presented解决方案是你想要的,所以在这里你的数据的跨pretation以及如何建立一个冗余数据集。

这是一部分,在那里我改变了code为:

 细节:[{
    充值卡号码:[34,22,23],
    供应商:麦,山姆,琼斯],
    说明:嗒嗒,blach,bluch],
    量:45555,232,345346]
}]


  

详细每个属性包含具有相同长度(希望)阵列。我想,每一个索引所属在一起,就像 34,吉米,胡说,45555


  
  

有关它的code部分会在详细信息键,然后属性的每个数组的长度。然后,它遍历长度和键并指定键/值对。对一个索引的所有值被收集之后,该对象被推向结果阵列


\r
\r

VAR ALLDATA = [{时期:4,uploadDate:2015年-11-19T21:00:00.000Z,节:[{转移code:8675309,细节:[{充值卡号码:[34,22,23],供应商: 吉米,山姆,琼斯],说明:嗒嗒,blach,bluch],量:45555,232,345346]}]}]},{期:4,uploadDate:2015-11-19T21:00:00.000Z,节:[{转移code:45576543,细节:[{充值卡号码:[22 ,33],供应商:[琼森,龙森],说明:陷阱音乐,耶稣],量:[12345,23234]}]}]}];\r
\r
功能newArray(数据){\r
    变种newData = [];\r
    data.forEach(函数(顶部){\r
        top.section.forEach(函数(MID){\r
            mid.details.forEach(功能(低){\r
                VAR键= Object.keys(低),\r
                    长度= keys.reduce(功能(R,A){返回Math.max(R,低[α]。长度);},0),\r
                    OBJ,I = 0;\r
                而(I<长度){\r
                    OBJ = {\r
                        uploadDate:top.uploadDate,\r
                        时间:top.period\r
                    };\r
                    keys.forEach(函数(K){\r
                        OBJ [K] =低[K] [I]\r
                    });\r
                    newData.push(OBJ);\r
                    我++;\r
                }\r
            });\r
        });\r
    });\r
    返回newData;\r
}\r
\r
的document.write('< pre>'+ JSON.stringify(newArray(ALLDATA),0​​,4)+'< / pre>');

\r

\r
\r

I have a multi-dimensional array of objects that is wreaking havoc on Angular's ng-repeat, so I want to convert it to a single-dimensional array. I tried and failed to do so using forEach. When I try to use console.log(newData) after running the function, it returns an empty array. Am I making it too simple, or did I make a stupid mistake?

//Sample data (allData):

var allData = [{
  "period": 4,
  "uploadDate": "2015-11-19T21:00:00.000Z",
  "section": [{
    "transferCode": 8675309,
    "details": [{
      "voucherNumber": [34, 22],
      "vendor": "jimmy",
      "description": "blah      ",
      "amount": "t        45,555.00"
    }]
  }]
}, {
  "period": 4,
  "uploadDate": "2015-11-19T21:00:00.000Z",
  "section": [{
    "transferCode": 45576543,
    "details": [{
      "voucherNumber": [22, 33],
      "vendor": "Jonson",
      "description": "trap music      ",
      "amount": "t        12,345.00"
    }]
  }]
}]

//code to push to new array:

var newData = [];

function newArray() {
  allData.forEach(function(top) {
    top.section.forEach(function(mid) {
      mid.details.forEach(function(low) {
        newData.push({
          vendor: low.vendor,
          voucherNumber: low.voucherNumber,
          description: low.description,
          amount: low.amount,
          transferCode: mid.transferCode,
          uploadDate: top.uploadDate,
          period: top.period
        })
      })
    })
  })
  return newData;
}
newArray();
document.body.textContent = JSON.stringify(newData);

解决方案

I am not sure, that your presented solution is what you want, so here an interpretation of your data and how to build an redundant data set.

This is the part, where I changed the code for:

"details": [{
    "voucherNumber": [34, 22, 23],
    "vendor": ["jimmy", "sam", "jones"],
    "description": ["blah", "blach", "bluch"],
    "amount": [45555, 232, 345346]
}]

Every property in details contains an array with the same length (hopefully). I assume, that every index belongs together, like 34, jimmy, blah, 45555.

The code part for it looks for the keys in details and then for the length of every array of the properties. Then it loops over the length and the keys and assign the key/value pairs. After all values for one index are collected, the object is pushed to the result array.

var allData = [{ "period": 4, "uploadDate": "2015-11-19T21:00:00.000Z", "section": [{ "transferCode": 8675309, "details": [{ "voucherNumber": [34, 22, 23], "vendor": ["jimmy", "sam", "jones"], "description": ["blah", "blach", "bluch"], "amount": [45555, 232, 345346] }] }] }, { "period": 4, "uploadDate": "2015-11-19T21:00:00.000Z", "section": [{ "transferCode": 45576543, "details": [{ "voucherNumber": [22, 33], "vendor": ["Jonson", "ronson"], "description": ["trap music", "jesus"], "amount": [12345, 23234] }] }] }];

function newArray(data) {
    var newData = [];
    data.forEach(function (top) {
        top.section.forEach(function (mid) {
            mid.details.forEach(function (low) {
                var keys = Object.keys(low),
                    length = keys.reduce(function (r, a) { return Math.max(r, low[a].length); }, 0),
                    obj, i = 0;
                while (i < length) {
                    obj = {
                        uploadDate: top.uploadDate,
                        period: top.period
                    };
                    keys.forEach(function (k) {
                        obj[k] = low[k][i];
                    });
                    newData.push(obj);
                    i++;
                }
            });
        });
    });
    return newData;
}

document.write('<pre>' + JSON.stringify(newArray(allData), 0, 4) + '</pre>');

这篇关于转换对象的多维数组对单维阵列的对象的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆