加入嵌套数组
Join nested array
我从 Q.all()
得到一个结果,是这样的 -
promise = [Arr1,Arr2,Arr3....]
每个 Arr
可以是 null
或普通 JS 对象数组。
我想将所有这些数组合并为一个大数组;
我可以循环使用数组 concat
方法来加入它们。
有没有其他内置在 JS 中的优雅解决方案?
这是示例数组 -
[
{
"endDate": "2015-06-11 14:52:00",
"quantity": 75,
},
{
"endDate": "2015-06-11 14:42:00",
"quantity": 78,
},
{
"endDate": "2015-06-01 14:43:00",
"quantity": 69,
},
{
"endDate": "2015-05-14 13:38:00",
"quantity": 85,
}
]
I have these libraries available as well lodash
, angular
我相信这将是 flattenDeep
(to do the flattening) and without
的组合(删除 null
s — 至少,我认为你想删除 null
s;如果不是,请取 without
出):
var result = _.without(_.flattenDeep(yourArray), null);
实例:
// NOTE: You said you had an array with arrays in it, so I've taken
// the one array you gave and used it as two entries in the array
// below (with some minor mods). Note also the nulls.
var yourArrays = [
[
{
"endDate": "2015-06-11 14:52:00",
"quantity": 75
},
{
"endDate": "2015-06-11 14:42:00",
"quantity": 78
},
{
"endDate": "2015-06-01 14:43:00",
"quantity": 69
},
{
"endDate": "2015-05-14 13:38:00",
"quantity": 85
}
],
null,
null,
[
{
"endDate": "2015-07-11 14:52:00",
"quantity": 12
},
{
"endDate": "2015-07-11 17:42:00",
"quantity": 34
},
{
"endDate": "2015-07-01 13:43:00",
"quantity": 56
},
{
"endDate": "2015-08-14 12:38:00",
"quantity": 85
}
]
];
var result = _.without(_.flattenDeep(yourArrays), null);
document.body.insertAdjacentHTML(
"beforeend",
"<pre>" + JSON.stringify(result, null, 2) + "</pre>"
);
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
你可以简单地使用.map
功能。
var promise = [[0,1],[2,3],[],[6,7]]
var combineArr = promise.map(function(value, index, array) {
return array[index];
});
alert(combineArr);
我从 Q.all()
得到一个结果,是这样的 -
promise = [Arr1,Arr2,Arr3....]
每个 Arr
可以是 null
或普通 JS 对象数组。
我想将所有这些数组合并为一个大数组;
我可以循环使用数组 concat
方法来加入它们。
有没有其他内置在 JS 中的优雅解决方案?
这是示例数组 -
[
{
"endDate": "2015-06-11 14:52:00",
"quantity": 75,
},
{
"endDate": "2015-06-11 14:42:00",
"quantity": 78,
},
{
"endDate": "2015-06-01 14:43:00",
"quantity": 69,
},
{
"endDate": "2015-05-14 13:38:00",
"quantity": 85,
}
]
I have these libraries available as well
lodash
,angular
我相信这将是 flattenDeep
(to do the flattening) and without
的组合(删除 null
s — 至少,我认为你想删除 null
s;如果不是,请取 without
出):
var result = _.without(_.flattenDeep(yourArray), null);
实例:
// NOTE: You said you had an array with arrays in it, so I've taken
// the one array you gave and used it as two entries in the array
// below (with some minor mods). Note also the nulls.
var yourArrays = [
[
{
"endDate": "2015-06-11 14:52:00",
"quantity": 75
},
{
"endDate": "2015-06-11 14:42:00",
"quantity": 78
},
{
"endDate": "2015-06-01 14:43:00",
"quantity": 69
},
{
"endDate": "2015-05-14 13:38:00",
"quantity": 85
}
],
null,
null,
[
{
"endDate": "2015-07-11 14:52:00",
"quantity": 12
},
{
"endDate": "2015-07-11 17:42:00",
"quantity": 34
},
{
"endDate": "2015-07-01 13:43:00",
"quantity": 56
},
{
"endDate": "2015-08-14 12:38:00",
"quantity": 85
}
]
];
var result = _.without(_.flattenDeep(yourArrays), null);
document.body.insertAdjacentHTML(
"beforeend",
"<pre>" + JSON.stringify(result, null, 2) + "</pre>"
);
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
你可以简单地使用.map
功能。
var promise = [[0,1],[2,3],[],[6,7]]
var combineArr = promise.map(function(value, index, array) {
return array[index];
});
alert(combineArr);