且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串转换为对象数组

更新时间:2022-11-19 09:14:28

所以 String.split 返回一个由(但不包括)您传入的是分割字符串,所以 box [0] 是您的开始,而 box [1] 是您的开始结束。然后,您只需要返回要映射到字符串项目的对象即可。

So String.split returns an array of your strings split by (and not including) the split-string you pass in, so box[0] is your start, and box[1] would be your end. Then, you just need to return the object that is to be mapped to your string item.

var splitting_array = ["0900-1000", "1200-1300"];
var split_objects = splitting_array.map(function(str) {
  var box = str.split('-');
  return {start: box[0], end: box[1]}
});
console.log(split_objects); // [{start:"0900", end:"1000"}, {start:"1200", end:"1300"}]