[Javascript] Implement zip function
1. Use a for loop to traverse the videos and bookmarks array at the same time. For each video and bookmark pair, create a {videoId, bookmarkId} pair and add it to the videoIdAndBookmarkIdPairs array.
function() { var videos = [ { "id": 70111470, "title": "Die Hard", "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 4.0, }, { "id": 654356453, "title": "Bad Boys", "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 5.0, }, { "id": 65432445, "title": "The Chamber", "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 4.0, }, { "id": 675465, "title": "Fracture", "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 5.0, } ], bookmarks = [ {id: 470, time: 23432}, {id: 453, time: 234324}, {id: 445, time: 987834} ], counter, videoIdAndBookmarkIdPairs = []; for(counter = 0; counter < Math.min(videos.length, bookmarks.length); counter++) { videoIdAndBookmarkIdPairs.push({ videoId: videos[counter].id, bookmarkId: bookmarks[counter].id }) } return videoIdAndBookmarkIdPairs; }
2. Let's add a static zip() function to the Array type. The zip function accepts a combiner function, traverses each array at the same time, and calls the combiner function on the current item on the left-hand-side and right-hand-side. The zip function requires an item from each array in order to call the combiner function, therefore the array returned by zip will only be as large as the smallest input array.
// JSON.stringify(Array.zip([1,2,3],[4,5,6], function(left, right) { return left + right })) === '[5,7,9]' Array.zip = function(left, right, combinerFunction) { var counter, results = []; for(counter = 0; counter < Math.min(left.length, right.length); counter++) { // Add code here to apply the combinerFunction to the left and right-hand items in the respective arrays results.push(combinerFunction(left[counter], right[counter])) } return results; };
3. Let's repeat exercise 1, but this time lets use your new zip() function. For each video and bookmark pair, create a {videoId, bookmarkId} pair.
function() { var videos = [ { "id": 70111470, "title": "Die Hard", "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 4.0, }, { "id": 654356453, "title": "Bad Boys", "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 5.0, }, { "id": 65432445, "title": "The Chamber", "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 4.0, }, { "id": 675465, "title": "Fracture", "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg", "uri": "http://api.netflix.com/catalog/titles/movies/70111470", "rating": 5.0, } ], bookmarks = [ {id: 470, time: 23432}, {id: 453, time: 234324}, {id: 445, time: 987834} ]; return Array. zip( videos, bookmarks, (video,bookmark ) => ({videoId: video.id, bookmarkId: bookmark.id})) }
相关文章
- javascript 基础_JavaScript高级编程
- JavaScript对象属性是有序的吗?
- JavaScript继承和原型链
- javascript使用正则表达式替换或者捕获子字符串
- JavaScript学习总结(二十)——Javascript非构造函数的继承详解编程语言
- Linux 安装 Zip 命令指南(linux安装zip命令)
- javascript json详解编程语言
- Linux安装Zip压缩工具的指南(linux安装zip)
- MySQL解压一个Zip文件的方法(mysql zip解压后)
- 用Mysql轻松压缩和解压Zip网址(mysql zip网址)
- MySQL数据库备份使用ZIP文件压缩并设置密码(mysql zip 密码)
- MySQL下载轻松获得ZIP版本(mysql下载下了zip)
- Javascript不能释放内存.
- 一个加密JavaScript的开源工具PACKER2.0.2
- Javascript更新JavaScript数组的uniq方法
- javascript学习之旅(3)
- javascript变量作用域代码分析
- Javascript在IE或Firefox下获取鼠标位置的代码
- JavaScript对Cookie操作的封装小结
- javascript小型动画组件与实现代码
- JavaScript布尔操作符解析&&||!
- javascript实现拖动层效果代码(符合标准且兼容IE,chrome,firefox)
- JavaScript中伪协议javascript:使用探讨