var getTimes = [{timeStart: ' ',timeEnd: ' ',timeStartLater: "", timeEndLater: ""},
{timeStart: '9:00',timeEnd: '10:00',timeStartLater: "", timeEndLater: ""},
{timeStart: '10:30',timeEnd: '12:00',timeStartLater: "", timeEndLater: ""},
{timeStart: '',timeEnd: '',timeStartLater: "", timeEndLater: ""}]
var cc = [
{ timeStart: '10:30', timeEnd: '12:00'},
{ timeStart: '16:00', timeEnd: '17:00' }
]
var lastArr = [{timeStart: ' ',timeEnd: ' ',timeStartLater: "", timeEndLater: ""},
{timeStart: '9:00',timeEnd: '10:00',timeStartLater: "", timeEndLater: ""},
{timeStart: '10:30',timeEnd: '12:00',timeStartLater: "10:30", timeEndLater: "12:00"},
{timeStart: '',timeEnd: '',timeStartLater: "16:00", timeEndLater: "17:00"}]
数组getTimes和数组cc合并得到数组lastArr,就是在cc[i].timeStart == getTimes[j].timeStart的时候把cc[i].timeStart的值给getTimes[j]. timeStartLater,cc[i].timeEnd的值给getTimes[j]. timeEndLater
首先定义一个函数,记录第一次相等的索引.然后赋值就行了。
function findIndex(arr1,arr2){
var n;
for(var i = 0;i<arr1.length;i++){
for(var j = 0;j<arr2.length;j++){
if(arr1[i].timeStart==arr2[j].timeStart){
n = i;
return n;
}
}
}
}
var getTimes = [{timeStart: ' ',timeEnd: ' ',timeStartLater: "", timeEndLater: ""},
{timeStart: '9:00',timeEnd: '10:00',timeStartLater: "", timeEndLater: ""},
{timeStart: '10:30',timeEnd: '12:00',timeStartLater: "", timeEndLater: ""},
{timeStart: '',timeEnd: '',timeStartLater: "", timeEndLater: ""}]
var cc = [
{ timeStart: '10:30', timeEnd: '12:00'},
{ timeStart: '16:00', timeEnd: '17:00' }
]
var index = findIndex(getTimes,cc);//调用函数记录索引
console.log(index);
var j = 0;
for(index;index<getTimes.length;index++){
getTimes[index].timeStartLater=cc[j].timeStart;
getTimes[index].timeEndLater=cc[j].timeEnd;
j++;
}
console.log(getTimes);
循环到循环,对比,赋值啊
不对啊,要是就(循环到循环,对比,赋值)那么简单我就不提问了啊
{timeStart: '',timeEnd: '',timeStartLater: "16:00", timeEndLater: "17:00"}
这个如何得到的?
数组cc的第二项开始时间,结束时间赋值给它的
问题的意思应该是只要找到相等的,就把cc整个数组从getTimes的当前位置一一添加进去。
遍历getTimes,再其中遍历cc比较timeStart,如果相等就给timeStartLater和timeendLater赋值呗
另外你getTime最后一天数据没有timeStart为什么也会给这两个变量赋值
就是要把cc的timeStart和timeEnd赋值给它
@青青子衿619: 不是给两个later赋值么