Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用普通javascript重新组织json数据_Javascript - Fatal编程技术网

使用普通javascript重新组织json数据

使用普通javascript重新组织json数据,javascript,Javascript,我使用的数据来自一个web服务,该服务发送具有以下结构的JSON数据。你可以在这里看到我的名字 问题:我目前的尝试非常缓慢。而且,只有在所有日期的报告次数相同的情况下,它才有效。例如,在下面的记录中,2018-08-01和2018-07-31分别有两个时间文件和一个时间文件,但在重新组织后,第31个时间文件仍显示两个记录。希望我说的有道理 function dbDateTimeToDateTme(value){ /* value isforamtted as 2018-08-04T22

我使用的数据来自一个web服务,该服务发送具有以下结构的JSON数据。你可以在这里看到我的名字

问题:我目前的尝试非常缓慢。而且,只有在所有日期的报告次数相同的情况下,它才有效。例如,在下面的记录中,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']));
或:


我在这里添加了。

您可以使用reduce大大提高可读性。用于在数组中查找已存在的对象,并相应地更新它们:

const数据={
“数据”:[
{
“费用”:“1026.2100”,
“收入”:“869.4500”,
“位置标识”:1,
“截止日期”:“2018-07-31T04:00:00Z”,
“商店”:“东部省”
},
{
“费用”:“1026.3300”,
“收入”:“869.0300”,
“位置标识”:1,
“截止日期”:“2018-08-01T00:00:00Z”,
“商店”:“东部省”
},
{
“费用”:“1026.7600”,
“收入”:“870.2000”,
“位置标识”:1,
“截止日期”:“2018-08-01T04:00:00Z”,
“商店”:“东部省”,
},
]
};
const res=data.data.reduce((对象,项)=>{
//我们在最终阵列中已经有这个位置了吗?
const foundItem=obj.find((i)=>i.location\u id==item.location\u id);
const date=子项的项(0,10);
常数时间=子日期(11,8)的项目;
//我们把日期加上去吧
如果(项目){
//搜索当前日期
const foundDate=foundItem.dates.find((i)=>i.date==date);
//数组中是否已经有日期?
如果(创建日期){
//将时间推到数组中
foundDate.main.push({
时间
费用:item.expense,
收入:项目.收入
});
}否则{
//使用此时间推送新的日期对象
foundItem.dates.push({
日期,
主要内容:[{
时间
费用:item.expense,
收入:项目.收入
}]
});
}
}否则{
//推一个全新的位置
推({
商店:item.store,
位置\u id:item.location\u id,
日期:[
{
日期,
主要内容:[{
时间
费用:item.expense,
收入:项目.收入
}]
}
]
});
}
返回obj;
}, []);

控制台日志(res)如果不想进行大量迭代,可以使用reduce,使用id和date作为键值,然后可以使用map或其他函数释放结果

const数据=[
{
“费用”:“1026.2100”,
“收入”:“869.4500”,
“位置标识”:1,
“截止日期”:“2018-07-31T04:00:00Z”,
“商店”:“东部省”
},
{
“费用”:“1026.3300”,
“收入”:“869.0300”,
“位置标识”:1,
“截止日期”:“2018-08-01T00:00:00Z”,
“商店”:“东部省”
},
{
“费用”:“1026.7600”,
“收入”:“870.2000”,
“位置标识”:1,
“截止日期”:“2018-08-01T04:00:00Z”,
“商店”:“东部省”,
},
];
const result=data.reduce((acum={},当前)=>{
const year=当前日期切片(0,10);
const hour=当前日期片(11,19);
如果(!acum[当前位置\u id]){
acum[当前位置\u id]={
商店:current.store,
位置\u id:current.location\u id,
日期:{
[当前日期]:{
日期:当前。对于_日期,
主要内容:[
{
时间:'',
收入:当期收入,
费用:本期费用,
},
],
}
}
}
}如果(!acum[current.location\u id].日期[year]){
acum[当前位置\u id]。日期[年]={
日期:年月日,
主要内容:[
{
时间:'',
收入:当期收入,
费用:本期费用,
},
],
}
}否则{
acum[current.location\u id]。日期[year]。main.push({
时间:'',
收入:当期收入,
费用:本期费用,
});
}
回归acum;
}, {});
控制台日志(结果);
console.log('-----------')
让arr=Object.keys(result.map)(key=>{
设res=结果[键]
res.dates=Object.keys(结果[key].dates).map(日期=>result[key].dates[date])
返回res;
});

console.log(arr)
请显示迄今为止您试图操纵该对象的代码@杰夫,这是es6console中的怪物。com@FrankerZ啊,好的,外部来源,没有检查。。。啊,看到了。好吧,把相关的代码放在问题里。我们不应该仅仅为了审查您的问题而离开现场。演示很棒,但只是作为对问题中实际存在的what的支持。谢谢Frankerz!稍微调整一下,效果就很好了。@NieSelam这是什么小调整?在区域://推送一个全新的位置中,您添加了日期:[[{,这与其他两个块不同。我的模板很混乱。我只需要做日期:[{@NieSelam是的,错过了。子数组有点混乱。已更新
[{

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