简体   繁体   English

如何遍历对象数组和数组的嵌套数组

[英]how to loop through array of objects and separate nested array of arrays

I have JSON that I currently use for a kendo-ui chart. 我有目前用于kendo-ui图表的JSON。 I need to use the data for a grid so I need to separate the nested data array of arrays into there own object. 我需要将数据用于网格,因此需要将数组的嵌套数据数组分离为自己的对象。 Javascript or linq.js will work fine. Javascript或linq.js可以正常工作。 here is the JSON I am starting with. 这是我开始使用的JSON。

customSeries = [{
"name": "Chantal Hamlet - Green Castle Homes",
"subId": "10223",
"bldId": "13551",
"data": [
    [179900, 1386],
    [214900, 1440],
    [194500, 1496],
    [217900, 1504],
    [189900, 1542],
    [184900, 1546],
    [192500, 1570]
],

}, {
"name": "Ella Sea Condos - Sahnow Construction",
"subId": "9761",
"bldId": "27380",
"data": [
    [199900, 1500]
]
}, {
"style": "smooth",
"color": "blue",
"data": [
    [20000, 200],
    [40000, 400],
    [40000, 400]
],
"name": "Subject Property"
}]

I need to end up with 2 separate arrays. 我需要结束于2个单独的数组。

First Array 第一阵列

Array1 = [{
"name": "Chantal Hamlet - Green Castle Homes",
"subId": "10223",
"bldId": "13551"

}, {
  "name": "Ella Sea Condos - Sahnow Construction",
  "subId": "9761",
  "bldId": "27380"
}, {
"style": "smooth",
"color": "blue",
"name": "Subject Property"
}]

Second Array 第二阵列

Array2 = [
{
    "data": [
        [179900, 1386],
        [214900, 1440],
        [194500, 1496],
        [217900, 1504],
        [189900, 1542],
        [184900, 1546],
        [192500, 1570]
    ]

}, {
    "data": [
        [199900, 1500]
    ]
}, {
    "data": [
        [20000, 200],
        [40000, 400],
        [40000, 400]
    ]
}

] ]

You could use Array.prototype.map method. 您可以使用Array.prototype.map方法。

var Array1 = customSeries.map(function(el) {
  return {
    name: el.name,
    subId: el.subId,
    bldId: el.bldId
  };
});

var Array2 = customSeries.map(function(el) {
  return {
    data: el.data
  };
});

Update: 更新:

The above code not work when your elements in customSeries don't have fixed keys except data . customSeries的元素没有data以外的固定键时,以上代码不起作用。

If you using lodash , you could do this: 如果使用lodash ,则可以执行以下操作:

var Array1 = customSeries.map(function(el) {
  return _.omit(el, 'data');
});

var Array2 = customSeries.map(function(el) {
  return _.pick(el, 'data');
});

Depending on the length of the array, you might want to do this in a single pass. 根据数组的长度,您可能希望一次完成此操作。 Using linq.js here won't help much for this, it'll just be added overhead with no real benefit. 在这里使用linq.js并没有多大帮助,只会增加开销,而没有真正的好处。

If you don't mind obliterating your original data, you can process them both simultaneously by going through each of the items and add a copy of the data array to the second array while deleting the data from the item. 如果您不希望删除原始数据,则可以通过遍历每个项目来同时处理它们,并在从项目中删除数据的同时将data数组的副本添加到第二个数组中。

var array1 = data,
    array2 = [];
array1.forEach(function (item) {
    array2.push({
        data: item.data
    });
    delete item.data;
});

If you'd rather keep your original data, you'll have to clone each item as you process them. 如果您希望保留原始数据,则必须在处理它们时克隆每个项目。

var array1 = [],
    array2 = [];
data.forEach(function (item) {
    var noData = yourCloneMethod(item); // replace call with preferred clone method
    delete noData.data;
    array1.push(noData);
    array2.push({
        data: item.data
    });
});

您可能想研究使用lodash ,它具有许多出色的函数,可用于处理数组和执行MapReduce函数。

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

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