后台给了我一个数据结构作为表头,我感觉不太好用,想修改一下格式。 var dateInfo = { week1_end: "2018-09-09", week1_start: "2018-09-03", week2_end: "2018-09-16", week2_start: "2018-09-10", week3_end: "2018-09-23", week3_start: "2018-09-17", week4_end: "2018-09-30", week4_start: "2018-09-24", week5_end: "2018-10-07", week5_start: "2018-10-01"};这些不太好直接循环渲染到页面上。我想大概修改成这样的结构好处理了dateInfo = [
{week1_start:"2018-09-03",week1_end:"week1_end"},
{week2_start:"2018-09-03",week2_end:"week1_end"} ]一开始的思路是获取下滑杠前面一样的做成一个对象最后放到数组里。但是每次打印发现都不是想象的那样var new_arr = []for(item in dateInfo){ // console.log(item)
var str1 = item.match(/(\S*)_/)[1]; if (item.includes(str1)) { var obj = {
item : dateInfo[item]
}
} new_arr.push(obj)
}为什么会这样啊????难道是我的思路错了吗
1 回答
烙印99
TA贡献1829条经验 获得超13个赞
代码确实有点问题,我写了一个
const temp = {};for (item in dateInfo) { const val = dateInfo[item] const str1 = item.match(/(\S*)_/)[1]; if (!temp[str1]) { temp[str1] = {}; temp[str1][item] = val; } else { temp[str1][item] = val } }const new_arr = Object.values(temp);console.log(new_arr);
添加回答
举报
0/150
提交
取消