Using the method of reduce to implement object array de-weight

Source: Internet
Author: User

In the development and interview, the problem of array de-weight is often favored by the beloved, the shortest code to solve the problem will make the efficiency of a greater increase. Normal array, we can use the filter method to go to the weight, details see my blog: http://www.cnblogs.com/mmykdbc/p/8422419.html

When it is an array of objects, filter is dwarfed, and then with that kind of stupid one traversal method will make the work efficiency greatly reduced, this time, need to use the reduce overlay method, the code:

  Let arr = [{' All ': 1}, {' All ': 2}, {' All ': 3}, {' All ': 4}, {"All": 1}];  Let hash = {};  arr = Arr.reduce (item, Next) = {    Hash[next.all]? "": hash[next.all] = True && Item.push (next);    Return item  }, []);  Console.log (arr);

Results:

Using the method of reduce to implement object array de-weight

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.