简体   繁体   English

将多维对象数组转换为一维对象数组Javascript

[英]Convert a multi-dimensional array of objects to single-dimensional array of objects Javascript

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. 我有一个对象的多维数组,这对Angular的ng-repeat造成了严重破坏,因此我想将其转换为一维数组。 I tried and failed to do so using forEach . 我尝试使用forEach失败了。 When I try to use console.log(newData) after running the function, it returns an empty array. 当我在运行函数后尝试使用console.log(newData) ,它将返回一个空数组。 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); 

This might help eleviate you from being too literal... Note that the container arrays are skipped (eg "section" and "details") 这可以帮助您避免过于直白。请注意,容器数组将被跳过(例如“ section”和“ details”)

// Helper functions
function isArray(obj) {return Array.isArray ? Array.isArray(obj) : Object.prototype.toString.call(obj) === '[object Array]';}
function isNumber(val) { return Object.prototype.toString.call(val) === '[object Number]' && isFinite(val);}


// Flatten object
function flatten(arg, out) {
    out = out || {};
    for (var prop in arg) {
        var val = arg[prop];

        if (isArray(val)) {
            for (var i = 0; i < val.length; i++) {

                // introspect if the array is just an array of numbers
                var valin = val[i];
                if (isArray(val) && isNumber(valin)) {

                    // just set to the number array and move along.
                    out[prop] = val;
                    break;
                } else {
                    flatten(valin, out);
                }
            }

        // continue looking deeper
        } else if (typeof val == "object") {
            flatten(val, out); 

        // get the value
        } else {
            out[prop] = val; 
        }
    }
    return out;
}


// Put each flattened object into a new data array
var data = [];
for (var i = 0; i < allData.length; i++) {
    data.push(flatten(allData[i]));
}

console.log(JSON.stringify(data));

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). details中的每个属性都包含一个具有相同长度的数组(希望如此)。 I assume, that every index belongs together, like 34, jimmy, blah, 45555 . 我假设每个索引都属于34, jimmy, blah, 45555类,例如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. 它的代码部分在details查找键,然后在属性的每个数组中查找长度。 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>'); 

Ok, got it working with some helpful advice and taking it in a bit of a different direction: 好的,让它与一些有用的建议一起工作,并朝着另一个方向发展:

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]
    }]
  }]
}];

var newData = [];

function newArray() {
  allData.forEach(function(top) {
    top.section.forEach(function(mid) {
      mid.details.forEach(function(low) {
        low.vendor.forEach(function(vend){
            low.voucherNumber.forEach(function(vouch) {
                low.description.forEach(function(desc) {
                    low.amount.forEach(function(amt) {
                        newData.push({
                            vendor: vend.vendor,
                            voucherNumber: vouch.voucherNumber,
                            description: desc.description,
                            amount: amt.amount,
                            transferCode: mid.transferCode,
                            uploadDate: top.uploadDate,
                            period: top.period
                        });
                    });
                });
            });
        });
      });
    });
  });
  return newData;
}
newArray();
document.body.textContent = JSON.stringify(newData);
// var test = JSON.stringify(newData);
// console.log(test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM