[Javascript] Implement zip function

Source: Internet
Author: User

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 () {varVideos = [            {                "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[c Ounter].id}) }returnVideoidandbookmarkidpairs;} 

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 th E current item on the Left-hand-side and Right-hand-side. The ZIP function requires an item from each of the array in order to call the Combiner function, therefore the array returned by Zip'll only is 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) {varcounter, 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 arraysResults.push (Combinerfunction (Left[counter], Right[counter])}returnresults;};

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 () {varVideos = [            {                "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}        ]; returnArray. zip (videos, bookmarks, (Video,bookmark) = ({videoId:video.id, bookmarkId:bookmark.id} )}

[Javascript] Implement zip function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.