我正在使用来自Web服务的数据,该服务发送具有以下结构的JSON数据。您可以在https://es6console.com/jkadxuk1/看到我。
问题:我目前的尝试非常缓慢。并且仅当所有日期具有相同的报告时间数时,它才有效。例如在下面的记录中,有两个时间文件用于2018-08-01,一个时间文件用于2018-07-31,但第31个文件在重新组织时仍显示两个记录。希望我有道理。
function dbDateTimeToDateTme(value){
/*
value isforamtted as 2018-08-04T22:00:00Z
Here, we split them
@output: an array of date and time. Note we don't validate
*/
reply=[];
if(value){
date=value.substring(0,10)
time=value.substring(11,19)
reply.push(date,time);
}
return reply;
}
function viewFullTablar(acquired_data){
total=acquired_data.length;
if(total==0){
return false;
}
data=[]; //reorganized data
location_name="";
location_code=0;
datetime=[];
for(var i=0;i<total;i++){
date_index=-1;
place_index=-1; //
location_name=acquired_data[i]['store']
location_code=acquired_data[i]['location_id']
datetime=dbDateTimeToDateTme(acquired_data[i]['for_date']); //0=date,1=time
//now check if we have already added the location by its location_code
for(var counter=0;counter<data.length;counter++){
if (data[counter]['location_id']==location_code){
place_index=counter;
break;
}
}
//do we add the place?
if(place_index==-1){
//yes add it
data.push(
{
'index':i,
'store':location_name,
'location_id':location_code,
'dates':[]
}
);
place_index=0; //first element now
}
//does the date exist yet or not?
date_blocks=data[place_index]['dates'];
for(counter=0;counter<date_blocks.length;counter++){
if (date_blocks[counter]['date']==datetime[0]){
date_index=counter;
break;
}
}
//add the date to the place or not?
if(date_index==-1){
data[place_index]['dates'].push(
{
'date':datetime[0],
'main':[]
}
);
date_index=0;
}
//now add time and weather details
data[place_index]['dates'][date_index]['main'].push(
{
'time':datetime[1],
'income':acquired_data[i]['income']
}
);
}
return data;
}
var data={
"data": [
{
"expense": "1026.2100",
"income": "869.4500",
"location_id": 1,
"for_date": "2018-07-31T04:00:00Z",
"store": "Eastern Province"
},
{
"expense": "1026.3300",
"income": "869.0300",
"location_id": 1,
"for_date": "2018-08-01T00:00:00Z",
"store": "Eastern Province"
},
{
"expense": "1026.7600",
"income": "870.2000",
"location_id": 1,
"for_date": "2018-08-01T04:00:00Z",
"store": "Eastern Province",
},
]
}
console.log(viewFullTablar(data['data']));
每天最多可以有8个不同小时的报告时间,但是最小时间甚至可以低至1或0(如果还没有归档的话)。
出于显示目的,我想从if中获取以下数组:
[{
store:"Eastern Province",
location_id:1,
dates:[
{
'date':'2018-07-31',
main:[
{'time':04:00:00,
'income':1026
}];
}];
}];
或:
East Province
2018-07-31
04:00:00 value value
09:00:00 value value
2018-08-01
09:00:00 value value
10:00:10 value value
我在这里添加了https://es6console.com/jkadxuk1/。
答案 0 :(得分:1)
您可以使用reduce大大提高可读性。使用.find()
浏览数组以查找已经存在的对象,并相应地更新它们:
const data = {
"data": [
{
"expense": "1026.2100",
"income": "869.4500",
"location_id": 1,
"for_date": "2018-07-31T04:00:00Z",
"store": "Eastern Province"
},
{
"expense": "1026.3300",
"income": "869.0300",
"location_id": 1,
"for_date": "2018-08-01T00:00:00Z",
"store": "Eastern Province"
},
{
"expense": "1026.7600",
"income": "870.2000",
"location_id": 1,
"for_date": "2018-08-01T04:00:00Z",
"store": "Eastern Province",
},
]
};
const res = data.data.reduce((obj, item) => {
// Do we already have this location in the final array?
const foundItem = obj.find((i) => i.location_id == item.location_id);
const date = item.for_date.substr(0,10);
const time = item.for_date.substr(11,8);
// Let's just append the date
if (foundItem) {
// Search for the current date
const foundDate = foundItem.dates.find((i) => i.date == date);
// Is there already a date in the array?
if (foundDate) {
// Push the time into the array
foundDate.main.push({
time,
expense: item.expense,
income: item.income
});
} else {
// Push a new date object, with this time
foundItem.dates.push({
date,
main: [{
time,
expense: item.expense,
income: item.income
}]
});
}
} else {
// Push a whole new location
obj.push({
store: item.store,
location_id: item.location_id,
dates: [
{
date,
main: [{
time,
expense: item.expense,
income: item.income
}]
}
]
});
}
return obj;
}, []);
console.log(res);
答案 1 :(得分:0)
如果您不想进行大量迭代,则可以使用reduce使用id和date作为键值,之后可以使用map或其他函数来展开结果
const data = [
{
"expense": "1026.2100",
"income": "869.4500",
"location_id": 1,
"for_date": "2018-07-31T04:00:00Z",
"store": "Eastern Province"
},
{
"expense": "1026.3300",
"income": "869.0300",
"location_id": 1,
"for_date": "2018-08-01T00:00:00Z",
"store": "Eastern Province"
},
{
"expense": "1026.7600",
"income": "870.2000",
"location_id": 1,
"for_date": "2018-08-01T04:00:00Z",
"store": "Eastern Province",
},
];
const result = data.reduce((acum = {}, current) => {
const year = current.for_date.slice(0, 10);
const hour = current.for_date.slice(11, 19);
if (!acum[current.location_id]) {
acum[current.location_id] = {
store: current.store,
location_id: current.location_id,
dates: {
[current.for_date]: {
date: current.for_date,
main: [
{
time: '',
income: current.income,
expense: current.expense,
},
],
}
}
}
} else if (!acum[current.location_id].dates[year]) {
acum[current.location_id].dates[year] = {
date: year,
main: [
{
time: '',
income: current.income,
expense: current.expense,
},
],
}
} else {
acum[current.location_id].dates[year].main.push({
time: '',
income: current.income,
expense: current.expense,
});
}
return acum;
}, {});
console.log(result);
console.log('---------')
let arr = Object.keys(result).map(key => {
let res = result[key]
res.dates = Object.keys(result[key].dates).map(date => result[key].dates[date])
return res;
});
console.log(arr)