Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

javascript - Compare the date value of an array of objects with the dates from an array of strings

I have the folowing use case and I am stack for days.

I have an array which contains dates in a string format like the following:

var rows = ["23-01-2021", "24-01-2021", "25-01-2021", "26-01-2021", "27-01-2021", "28-01-2021"]

I have also a data structure with the working days for each employee

var schedule = [{
    id: 1,
    name: "Chandler Bing",
    times: [

      {
        start: "9.00",
        end: "17.00",
        date: "25-01-2021"
      },
      {
        start: "9.00",
        end: "17.00",
        date: "26-01-2021"
      },


      {
        start: "9.00",
        end: "17.00",
        date: "28-01-2021"
      },
    ]
  },
  {
    id: 2,
    name: "Employee 2",
    times: [

      {
        start: "9.00",
        end: "17.00",
        date: "23-01-2021"
      },
      {
        start: "9.00",
        end: "17.00",
        date: "27-01-2021"
      },


      {
        start: "9.00",
        end: "17.00",
        date: "28-01-2021"
      }
    ]
  }
]

I would like to compare the array of dates with the array of times for each employee. If dates are missing from the array of times create an object with null values... Actually I would like to end up to a data structure like the following.

var schedule = [{
  id: 1,
  name: "Employee 1",
  times: [{
      start: "",
      end: "",
      date: ""
    }, {
      start: "",
      end: "",
      date: ""
    },
    {
      start: "9.00",
      end: "17.00",
      date: "25-01-2021"
    },
    {
      start: "9.00",
      end: "17.00",
      date: "26-01-2021"
    },
    {
      start: "",
      end: "",
      date: ""
    },


    {
      start: "9.00",
      end: "17.00",
      date: "28-01-2020"
    }
  ]
},
{
  id: 2,
  name: "Employee 2",
  times: [

    {
      start: "",
      end: "",
      date: ""
    },
    {
      start: "",
      end: "",
      date: ""
    },
    {
      start: "",
      end: "",
      date: ""
    },
    {
      start: "",
      end: "",
      date: ""
    },
    {
      start: "9.00",
      end: "17.00",
      date: "27-01-2021"
    },
    {
      start: "9.00",
      end: "17.00",
      date: "28-01-2021"
    },
  ]
}
]]

I have implemented this logic until now but obviosly is not working

var rows = ["23-01-2021", "24-01-2021", "25-01-2021", "26-01-2021", "27-01-2021", "28-01-2021"]
var schedule = [{
  id: 1,
  name: "Chandler Bing",
  times: [{
      start: "9.00",
      end: "17.00",
      date: "25-01-2021"
    },
    {
      start: "9.00",
      end: "17.00",
      date: "26-01-2021"
    },
    {
      start: "9.00",
      end: "17.00",
      date: "28-01-2021"
    },

  ]
}]


var arr = []
var test = {};

rows.forEach(function(row, index) {
  schedule.forEach(function(entry) {
    entry.times.forEach(function(times, index) {
      console.log(row)
      if (times.date === row) {
        test = {
          start: times.start,
          end: times.end,
          date: times.date
        }
        arr.push(test)
      } else {
        test = {
          start: "",
          end: "",
          date: ""
        }
        arr.push(test);
      }
    })
  })
})
console.log(arr);
question from:https://stackoverflow.com/questions/65901770/compare-the-date-value-of-an-array-of-objects-with-the-dates-from-an-array-of-st

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could map rows and have a look to times and take this obkject if date has the same value.

This approach features a closure over an index an needs sorted data of schedule's times array.

const
    rows = ["23-01-2021", "24-01-2021", "25-01-2021", "26-01-2021", "27-01-2021", "28-01-2021"],
    schedule = [{ id: 1, name: "Chandler Bing", times: [{ start: "9.00", end: "17.00", date: "25-01-2021" }, { start: "9.00", end: "17.00", date: "26-01-2021" }, { start: "9.00", end: "17.00", date: "28-01-2021" }] }, { id: 2, name: "Employee 2", times: [{ start: "9.00", end: "17.00", date: "23-01-2021" }, { start: "9.00", end: "17.00", date: "27-01-2021" }, { start: "9.00", end: "17.00", date: "28-01-2021" }] }],
    result = schedule.map(o => ({ ...o, times: rows.map((i => date => o.times[i]?.date === date
       ? o.times[i++]
       : { start: "", end: "", date: "" }
    )(0)) }));

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...