[Javascript] Advanced Reduce:flatten, Flatmap and Reduceright

Source: Internet
Author: User

Learn a few advanced reduction Patterns:flatten allows your to merge a set of arrays into a single array, the dreaded flat Map allows you to convert a array of objects into a array of arrays which then get flattened, and reduceright allows you To invert the order in which your reducer are applied to your input values.

Flatten
var data = [[4,5,6], [[7,8,9]]; var flatdata = Data.reduce (ACC, value) = {  return//[1, 2, 3, 4, 5, 6, 7, 8, 9]

Flatmap
varinput =[{title:"Batman begins", Year:2005, cast: ["Christian Bale",      "Michael Caine",      "Liam Neeson.",      "Katie Holmes",      "Gary Oldman.",      "Cillian Murphy"]}, {title:"The Dark Knight", Year:2008, cast: ["Christian Bale",      "Heath Ledger",      "Aaron Eckhart",      "Michael Caine",      "Maggie Gyllenhal",      "Gary Oldman.",      "Morgan Freeman"]}, {title:"The Dark Knight Rises", Year:2012, cast: ["Christian Bale",      "Gary Oldman.",      "Tom Hardy",      "Joseph Gordon-levitt",      "Anne Hathaway",      "Marion Cotillard",      "Morgan Freeman",      "Michael Caine"    ]  }];varFlatmapinput = Input.reduce (ACC, value) = ={Value.cast.forEach (star)={    if(Acc.indexof (Star) = = = 1) {Acc.push (star);  };    }); returnacc;}, []);//["Christian Bale", "Michael Caine", "Liam Neeson", "Katie Holmes", "Gary Oldman", "Cillian Murphy", "Heath Ledger", "A Aron Eckhart "," Maggie Gyllenhal "," Morgan Freeman "," Tom Hardy "," Joseph Gordon-levitt "," Anne Hathaway "," Marion Cotilla Rd "]

Reduceright
var countdown = [1,2,3,4, "5"]; var str = countdown.reduceright (ACC, value) ={  return ACC +""  // "54321"

[Javascript] Advanced Reduce:flatten, Flatmap and Reduceright

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.