在此示例中,我要使用splice将array2连接或追加到array1,它返回空数组,但它应该返回两个数组中的所有值
var obj = [{"id":"36","task_name":"task1","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"37","task_name":"task2","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"38","task_name":"task3","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"39","task_name":"task4","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"40","task_name":"task5","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"41","task_name":"task6","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"42","task_name":"task7","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"43","task_name":"task8","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"44","task_name":"task9","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"45","task_name":"task10","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var obj2 = [{"id":"46","task_name":"task11","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"47","task_name":"task12","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"48","task_name":"task13","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"49","task_name":"task14","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"50","task_name":"task15","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"51","task_name":"task16","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"52","task_name":"task17","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"53","task_name":"task18","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"54","task_name":"task19","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"55","task_name":"task20","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var arr = Object.values(obj);
var arr2 = Object.values(obj2);
var fin = arr.splice(10,0,...arr2)
console.log(fin );
答案 0 :(得分:2)
由于您已经使用了散布运算符,为什么不将它同时用于两个数组?另外,您不需要Object.values
,因为您是从数组开始的-如果这些是对象,则更合适。
var obj = [{"id":"36","task_name":"task1","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"37","task_name":"task2","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"38","task_name":"task3","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"39","task_name":"task4","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"40","task_name":"task5","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"41","task_name":"task6","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"42","task_name":"task7","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"43","task_name":"task8","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"44","task_name":"task9","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"45","task_name":"task10","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var obj2 = [{"id":"46","task_name":"task11","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"47","task_name":"task12","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"48","task_name":"task13","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"49","task_name":"task14","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"50","task_name":"task15","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"51","task_name":"task16","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"52","task_name":"task17","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"53","task_name":"task18","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"54","task_name":"task19","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"55","task_name":"task20","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var fin = [...obj,...obj2]
console.log(fin);
答案 1 :(得分:1)
您可以简单地执行此操作,而无需使用splice()
var obj = [{"id":"36","task_name":"task1","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"37","task_name":"task2","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"38","task_name":"task3","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"39","task_name":"task4","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"40","task_name":"task5","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"41","task_name":"task6","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"42","task_name":"task7","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"43","task_name":"task8","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"44","task_name":"task9","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"45","task_name":"task10","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var obj2 = [{"id":"46","task_name":"task11","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"47","task_name":"task12","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"48","task_name":"task13","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"49","task_name":"task14","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"50","task_name":"task15","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"51","task_name":"task16","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"52","task_name":"task17","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"53","task_name":"task18","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"54","task_name":"task19","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"55","task_name":"task20","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
console.log(obj.concat(obj2));
答案 2 :(得分:0)
我可以确认Array.concat()
似乎给出了预期的结果:
var obj = [{"id":"36","task_name":"task1","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"37","task_name":"task2","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"38","task_name":"task3","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"39","task_name":"task4","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"40","task_name":"task5","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"41","task_name":"task6","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"42","task_name":"task7","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"43","task_name":"task8","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"44","task_name":"task9","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"45","task_name":"task10","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var obj2 = [{"id":"46","task_name":"task11","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"47","task_name":"task12","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"48","task_name":"task13","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"49","task_name":"task14","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"50","task_name":"task15","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"51","task_name":"task16","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"52","task_name":"task17","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"53","task_name":"task18","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"54","task_name":"task19","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"55","task_name":"task20","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var fin = obj.concat(obj2)
console.log(fin);
答案 3 :(得分:0)
也许是这样的:
var obj = [{"id":"36","task_name":"task1","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"37","task_name":"task2","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"38","task_name":"task3","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"39","task_name":"task4","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"40","task_name":"task5","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"41","task_name":"task6","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"42","task_name":"task7","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"43","task_name":"task8","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"44","task_name":"task9","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"45","task_name":"task10","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
var obj2 = [{"id":"46","task_name":"task11","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"47","task_name":"task12","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"48","task_name":"task13","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"49","task_name":"task14","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"50","task_name":"task15","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"51","task_name":"task16","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"52","task_name":"task17","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"53","task_name":"task18","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"54","task_name":"task19","birth_date":"2018-06-15 00:00:00","is_completed":"0"},{"id":"55","task_name":"task20","birth_date":"2018-06-15 00:00:00","is_completed":"0"}];
[].push.apply(obj, obj2);
console.log(obj);