首页 新闻 会员 周边 捐助

jquery 合并处理数组

0
悬赏园豆:50 [已解决问题] 解决于 2018-12-26 15:05

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

青青子衿619的主页 青青子衿619 | 菜鸟二级 | 园豆:300
提问于:2018-07-02 15:38
< >
分享
最佳答案
0

首先定义一个函数,记录第一次相等的索引.然后赋值就行了。

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

收获园豆:50
你风致 | 老鸟四级 |园豆:2217 | 2018-08-01 10:11
其他回答(3)
0

循环到循环,对比,赋值啊

悟行 | 园豆:12559 (专家六级) | 2018-07-02 18:36

不对啊,要是就(循环到循环,对比,赋值)那么简单我就不提问了啊

支持(0) 反对(0) 青青子衿619 | 园豆:300 (菜鸟二级) | 2018-07-02 19:15
0
{timeStart: '',timeEnd: '',timeStartLater: "16:00", timeEndLater: "17:00"}

这个如何得到的?

jello chen | 园豆:7336 (大侠五级) | 2018-07-02 21:11

数组cc的第二项开始时间,结束时间赋值给它的

支持(0) 反对(0) 青青子衿619 | 园豆:300 (菜鸟二级) | 2018-07-02 21:50

问题的意思应该是只要找到相等的,就把cc整个数组从getTimes的当前位置一一添加进去。

支持(0) 反对(0) 你风致 | 园豆:2217 (老鸟四级) | 2018-08-01 10:19
0

遍历getTimes,再其中遍历cc比较timeStart,如果相等就给timeStartLater和timeendLater赋值呗

另外你getTime最后一天数据没有timeStart为什么也会给这两个变量赋值

小光 | 园豆:1766 (小虾三级) | 2018-07-03 09:22

就是要把cc的timeStart和timeEnd赋值给它

支持(0) 反对(0) 青青子衿619 | 园豆:300 (菜鸟二级) | 2018-07-03 10:35

@青青子衿619: 不是给两个later赋值么

支持(0) 反对(1) 小光 | 园豆:1766 (小虾三级) | 2018-07-03 10:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册