如何将对象数组转换为对象数组的数组?这是根据某些条件对原始数组中的对象进行分组。因此,原始数组中与某些条件类似的对象将放在子数组中,该子数组是新数组的元素。
我遇到初始空数组插入问题,不应该插入它,并保持子数组和临时变量的临时状态作为标准。
我在这里缺少什么?是否有更混乱,可能不那么迫切,更实用的方法来实现这一目标?
var transform = function (array) {
var rows = [];
var parts = [];
var lastDay = null;
var i = 0;
array.forEach(function(item) {
var currentDay = new Date(item.dt_text).getDay();
if (currentDay != lastDay) {
// not in same day row
rows.push(parts);
parts = [];
parts.push(item);
lastDay = currentDay;
i = rows.indexOf(parts);
return;
} else if (currentDay == lastDay){
parts.push(item);
return;
}
});
return rows;
},
要处理的样本数据此函数具有以下形式:
[
{
"dt":1442275200,
"main":{"temp":285.66,"temp_min":282.93,"temp_max":285.66,"pressure":899.08,"sea_level":1029.64,"grnd_level":899.08,"humidity":84,"temp_kf":2.73},
"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],
"clouds":{"all":0},
"wind":{"speed":1.18,"deg":34.0052},
"rain":{},
"sys":{"pod":"n"},
"dt_text":"2015-09-15 00:00:00"
},
{
"dt":1442275200,
"main":{"temp":285.66,"temp_min":282.93,"temp_max":285.66,"pressure":899.08,"sea_level":1029.64,"grnd_level":899.08,"humidity":84,"temp_kf":2.73},
"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],
"clouds":{"all":0},
"wind":{"speed":1.18,"deg":34.0052},
"rain":{},
"sys":{"pod":"n"},
"dt_text":"2015-09-15 00:00:00"
},
{
"dt":1442228400,
"main":{"temp":285.66,"temp_min":282.93,"temp_max":285.66,"pressure":899.08,"sea_level":1029.64,"grnd_level":899.08,"humidity":84,"temp_kf":2.73},
"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],
"clouds":{"all":0},
"wind":{"speed":1.18,"deg":34.0052},
"rain":{},
"sys":{"pod":"n"},
"dt_text":"2015-09-14 00:00:00"
}
]
答案 0 :(得分:1)
以下是两种方法。不知道为什么你有i
或为什么绑定this
因为你没有使用eithe。您也没有将最后一行项目编码到行上。
第一版试图保持你的语法。
var transform = function (array) {
var rows = []; // row array
var parts; // undefined parts array
var lastDay; // undefine last day
array.forEach(function (item) {
var currentDay = new Date(item.dt * 1000).getDay(); // get day. safari returns NaN for new Date(item.dt_txt).getDay(), where dt_txt is in format in the array of question. chrome is ok. so better use timestamp to getDay().
if (currentDay !== lastDay) { // if not the same as last
// sorry this test is wrong have removed
// if (parts === undefined) { // check if there is a parts array.
parts = []; // create it and
rows.push(parts); // push it onto the rows array.
//}
parts.push(item); // push the new item onto the parts array
lastDay = currentDay; // remeber the last day.
} else {
parts.push(item); // same day so just push it onto the parts array;
}
});
return rows;
}
版本2是我能想到的最有效的方式。
var transform = function (array) {
var rows = []; // row array
var row; // current row
var lastDay; // undefine last day
array.forEach(function (item) {
var currentDay = new Date(item.dt * 1000).getDay(); // get day.
// safari returns NaN for new Date(item.dt_txt).getDay(), where dt_txt is in format in the array of question. chrome is ok. so better use timestamp to getDay().
if (currentDay !== lastDay) { // if not the same as last
row = rows.push([item])-1; // push new array onto the rows array and get new row index.
lastDay = currentDay; // remeber the last day.
} else {
rows[row].push(item); // same day so just push it onto the parts array;
}
});
return rows;
}
答案 1 :(得分:0)
你的逻辑并不糟糕。
但如果您的所有数据都在同一天,那么您就忘了推行。
因此,您必须在rows.push(parts)
循环后添加array.forEach()
。
此外,很少有提示。
array.forEach().bind(this)
。parts.length
而不是parts != []
答案 2 :(得分:0)
这是一种利用Array.prototype.reduce
的方法(如果您的问题建议已根据 element.dt
订购了数组元素):
function transform(array) {
var groups = [[]];
var lastItemForLastGroup = array.reduce(function (previous, current) {
// continue population of the latest group
groups[groups.length - 1].push(previous);
if (previous.dt !== current.dt) {
// add a new group
groups.push([]);
}
// return current so that it will `previous` in the next iteration
return current;
});
groups[groups.length - 1].push(lastItemForLastGroup);
return groups;
}
对于了解reduce
方法的人来说,这很简单,也很有道理。否则它可能看起来不可读(然后我们会说它滥用Array.prototype.reduce
而不是:))。
使用以下作为测试数据:
var harry = [
// group 0
{
"dt": 1442275200,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-15 00:00:00"
},
{
"dt": 1442275200,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-15 00:00:00"
},
// group 1
{
"dt": 1442228400,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-14 00:00:00"
},
{
"dt": 1442228400,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-14 00:00:00"
},
{
"dt": 1442228400,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-14 00:00:00"
},
// group 2
{
"dt": 1442181600,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-13 00:00:00"
},
{
"dt": 1442181600,
"main": {"temp": 285.66, "temp_min": 282.93, "temp_max": 285.66, "pressure": 899.08, "sea_level": 1029.64, "grnd_level": 899.08, "humidity": 84, "temp_kf": 2.73},
"weather": [{"id": 800, "main": "Clear", "description": "sky is clear", "icon": "01n"}],
"clouds": {"all": 0},
"wind": {"speed": 1.18, "deg": 34.0052},
"rain": {},
"sys": {"pod": "n"},
"dt_text": "2015-09-13 00:00:00"
}
];
transform(harry);
返回3组,其中第一组包含两个项目,第二组包含3个项目,第三组包含两个预期的项目。