0
Follow
0
View

JavaScript data conversion

xiaoyudgx 注册会员
2023-02-27 08:08
Hello, I am asked to answer the little assistant, your question has been a little partner to help you answer, thank you for your support and attention to have asked to answer!
PS: Q&A VIP annual card [limited time plus: free IT technical books], for details > > > https://vip.csdn.net/askvip?utm_source=1146287632
c198905 注册会员
2023-02-27 08:08
lijie43011 注册会员
2023-02-27 08:08

Use map set to do, do not exist, first get, concatenate, then set.

dutefei 注册会员
2023-02-27 08:08
let res = req.reduce((result, item) => {
  let index = result.findIndex((data) => data.index === item.index);
  if (index < 0) {
    result.push({
      index: item.index,
      data: Array(6).fill(0),
    });
    index = result.length - 1;
  }
  let date = new Date(item.dates);
  result[index].data[date.getDay()] = item.total;
  return result;
}, []);
.
< !- - - - - >
dinwenjin110 注册会员
2023-02-27 08:08
, index: "1" , total: 3 }, { dates: "2023-02-02" , index: "2" , total: 0 }, { dates: "2023-02-03" , index: "1" , total: 6 }, { dates: "2023-02-03" , index: "2" , total: 7 }, { dates: "2023-02-03" , index: "3" , total: 3 }, { dates: "2023-02-03" , index: "4" , total: 5 }, { dates: "2023-02-04" , index: "2" , total: 2 }, { dates: "2023-02-04" , index: "3" , total: 7 }, { dates: "2023-02-04" , index: "4" , total: 1 }, { dates: "2023-02-04" , index: "5" , total: 4 }, { dates: "2023-02-05" , index: "4" , total: 3 }, { dates: "2023-02-05" , index: "5" , total: 2 }, { dates: "2023-02-05" , index: "6" , total: 7 }, { dates: "2023-02-05" , index: "7" , total: 1 }, { dates: "2023-02-06" , index: "1" , total: 7 }, { dates: "2023-02-06" , index: "2" , total: 4 }, { dates: "2023-02-06" , index: "5" , total: 3 }, { dates: "2023-02-06" , index: "6" , total: 11 }, { dates: "2023-02-07" , index: "3" , total: 123 }, { dates: "2023-02-07" , index: "4" , total: 7 }, { dates: "2023-02-07" , index: "6" , total: 99 }, { dates: "2023-02-07" , index: "7" , total: 2 }, ] ; let keys = Array.from(new Set(req.map(i => i.index)));//得到唯一index的值 let dates = Array.from(new Set(req.map(i => i.dates)));//得到唯一dates的值 let res = keys.map(index => { let data = dates.map(date => { let item = req.find(i => i.dates == date && i.index == index);//找到index和dates一样的项 return item ? item.total : 0 ;//找不到发返回0,否则返回total属性 } ) return { index , data } ; } ) console.log(res) /* let res=[ { index: "1" , data: [ 3 , 6 , 0 , 0 , 7 , 0 ] } , { index: "2" , data: [ 0 , 7 , 2 , 0 , 4 , 0 ] } , { index: "3" , data: [ 0 , 3 , 7 , 0 , 0 , 123 ] } , { index: "4" , data: [ 0 , 5 , 1 , 3 , 0 , 7 ] } , { index: "5" , data: [ 0 , 0 , 4 , 2 , 3 , 0 ] } , { index: "6" , data: [ 0 , 0 , 0 , 7 , 11 , 99 ] } , { index: "7" , data: [ 0 , 0 , 0 , 1 , 0 , 2 ] } , ];*/ .
< !- - - - - >
csddjj1199 注册会员
2023-02-27 08:08
< div class = " md_content_show“数据- v - 3967 e397 = " " >

< div class = " aw-list-img " > img

About the Author

Question Info

Publish Time
2023-02-27 08:08
Update Time
2023-02-27 08:08

Related Question