Example code for calculating duplicate values of two-dimensional arrays using Javascript and sample code for two-dimensional arrays

Source: Internet
Author: User

Example code for calculating duplicate values of two-dimensional arrays using Javascript and sample code for two-dimensional arrays

Preface

I encountered a problem recently. The requirement is to use Javascript to calculate the duplicate values of two-dimensional arrays. For example, there is a two-dimensional array below.

[[\ 'Error \ ', 3], [\ 'error \', 5], [\ 'error \ ', 6], [\ 'true \', 3], [\ 'true \ ', 1]

Repeated items \ 'error \ 'and \ 'true \' need to be calculated \',

Statistical Calculation Result:

[[\ 'Error \ ', 14], [\ 'true \', 4]

Implementation Code:

Var arr = [[\ 'error \ ', 3], [\ 'error \', 5], [\ 'error \ ', 6], [\ 'true \ ', 3], [\ 'true \', 1]; var obj ={}; var result = []; arr. forEach (function (arr) {obj [arr [0] = obj [arr [0]? Obj [arr [0] + arr [1]: arr [1];}); for (var I in obj) {result. push ([I, obj [I])}

Summary

The above is all about this article. I hope it will help you learn or work. If you have any questions, please leave a message.

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.