简单的“ if”语句

时间:2018-10-21 08:55:18

标签: javascript

最近,我一直在测试一些东西,并且坚持使用很少的代码。这是一个草图:

let data = [{
    date: "2018-10-09 18:00",
    temp: "+13"
  },
  {
    date: "2018-10-09 21:00",
    temp: "+12"
  },
  {
    date: "2018-10-10 00:00",
    temp: "+5"
  },
  {
    date: "2018-10-10 15:00",
    temp: "+18"
  },
  {
    date: "2018-10-11 00:00",
    temp: "+4"
  },
  {
    date: "2018-10-11 00:00",
    temp: "+4"
  }
];

let res = [];

function setData(data, day, time, id) {
  let a = {
    day: day,
    time: time,
    temp: data.temp
  }
  res[id].push(a);
}

function parse(data) {
  let day, time, id = -1;

  for (let i = 0; i < data.length; i++) {
    day = data[i].date.split(" ")[0];
    time = data[i].date.split(" ")[1];

    if (id !== 0 && id !== -1) {
      setData(data[i], day, time, id);
    } else {
      id++;
      res[id] = new Array();
      setData(data[i], day, time, id);
    }
  }

}

parse(data);
console.log(res);

我需要解析数据并以这种方式对不同的日期进行排序:

res = [[{day: 2018-10-09}, {day: 2018-10-09}], [{day: 2018-10-10}, {day: 2018-10-10}], [{day: 2018-10-11}, {day: 2018-10-11}]]

但是我的代码不正确,我做了很多尝试,但都失败了。请帮助我

3 个答案:

答案 0 :(得分:1)

您需要修改方法,没有逻辑可以将日期分组。请尝试以下代码:

let data = [
	{
		date: "2018-10-09 18:00",
		temp: "+13"
	},    	
	{
		date: "2018-10-10 00:00",
		temp: "+5"
	},
    {
		date: "2018-10-09 21:00",
		temp: "+12"
	},
	{
		date: "2018-10-10 15:00",
		temp: "+18"
	},
	{
		date: "2018-10-11 00:00",
		temp: "+4"
	},
	{
		date: "2018-10-11 00:00",
		temp: "+4"
	}
];

let res = {};

function setData(data, day, time){
	let a = {
		day: day,
		time: time,
		temp: data.temp
	}
	res[day].push(a);
}

function parse(data) {
	let day, time, output = [];
	
	for (let i = 0; i < data.length; i++) {
		day = data[i].date.split(" ")[0];
		time = data[i].date.split(" ")[1];
        res[day] = res[day] || [];
		setData(data[i], day, time);    		
	}
    let sortedDates = Object.keys(res).sort();
    for (let date of sortedDates) {
         output.push(res[date]);
    } 
    return output;
}

res = parse(data);
console.log(res);

如果您还想对内部数组进行排序,那么可以看看sort with compare function

答案 1 :(得分:1)

尝试一下,希望对您有所帮助!干杯

let data = [
	{
		date: "2018-10-09 18:00",
		temp: "+13"
	},
	{
		date: "2018-10-09 21:00",
		temp: "+12"
	},
	{
		date: "2018-10-10 00:00",
		temp: "+5"
	},
	{
		date: "2018-10-10 15:00",
		temp: "+18"
	},
	{
		date: "2018-10-11 00:00",
		temp: "+4"
	},
	{
		date: "2018-10-11 00:00",
		temp: "+4"
	}
];

let res = [];

function setData(data, day, time, id){
	let a = {
		day: day,
		time: time,
		temp: data.temp
	}
	res[id].push(a);
}

function parse(data) {
    let day, time, lastDay, id = 0;
	
    for (let i = 0; i < data.length; i++) {
		day = data[i].date.split(" ")[0];
		time = data[i].date.split(" ")[1];
      
        if (i == 0) {

            res.push([]);
            setData(data[i], day, time, id);

        } else {

            lastDay = data[i-1].date.split(" ")[0];
          
            if(day.localeCompare(lastDay) == 0) {
                // Same day

                setData(data[i], day, time, id);

            } else {
                // Another day
            
                id++;
                res.push([]);
                setData(data[i], day, time, id);
            }
         }
    }

}

parse(data);
console.log(res);

答案 2 :(得分:1)

使用Map的方法略有不同。

var data = [{ date: "2018-10-09 18:00", temp: "+13" }, { date: "2018-10-09 21:00", temp: "+12" }, { date: "2018-10-10 00:00", temp: "+5" }, { date: "2018-10-10 15:00", temp: "+18" }, { date: "2018-10-11 00:00", temp: "+4" }, { date: "2018-10-11 00:00", temp: "+4" }],
    result = Array.from(
        data
            .reduce(
                (m, { date, temp }) => (
                    ([day, time]) => m.set(day, (m.get(day) || []).concat({ day, time, temp })))
                    (date.split(' ')),
                new Map
            )
            .values(),
        a => a.sort((a, b) => a.time.localeCompare(b.time))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }