Js changes data structure

the background gave me a data structure as the header. I don"t feel very good to use it. I want to change the format.

  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"
};

these are not good to render directly to the page. I think the structure is probably modified to be easy to deal with

dateInfo = [
   {week1_start:"2018-09-03",week1_end:"week1_end"},
   {week2_start:"2018-09-03",week2_end:"week1_end"} ]

the idea at first is to get the same slider in front of it and make an object and then put it in the array. But every time I print, it"s not what I thought.

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)
}

Why is this happening? Am I thinking wrong?


There is something wrong with the

code. I wrote a

.
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);

can split unnecessary regular expressions, do not abuse.


var new_arr = [];
for (item in dateInfo) {
    var index = item.match(/(\d+)/)[1];
    var key = item.match(/_(\S*)/)[1];
    new_arr[index] = new_arr[index] ? new_arr[index] : {};
    new_arr[index][key] = dateInfo[item];
}
console.log(new_arr);

 const 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'
    };

    
    // 
    const cKeys = new Set();
    Object.keys(dateInfo).forEach(value => {
      const cKey = value.match(/(\S*)_/)[1];
      cKeys.add(cKey);
    });


    const newArray = [];
    for (const cKey of Array.from(cKeys)) {
      const obj = {};
      for (const key in dateInfo) {
        if (!dateInfo.hasOwnProperty(key)) {
          continue;
        }
        if (key.startsWith(cKey)) {
          obj[key] = dateInfo[key];
        }
      }
      newArray.push(obj);
    }

Thank you for your help. I hope I can have the opportunity to help others in the future.


let 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 = JSON.parse(`[${JSON.stringify(dateInfo).replace(/,("week[^1]_end)/g, '},{$1')}]`)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b31f23-34184.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b31f23-34184.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?