Posted in Javascript onJune 04, 2019
本文实例讲述了JS实现的对象去重功能。分享给大家供大家参考,具体如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>3water.com js 对象去重</title> </head> <body> <script> var obj = [ {year: '2013-2014', term: '1'}, {year: '2013-2014', term: '2'}, {year: '2013-2014', term: '3'}, {year: '2013-2014', term: '2'}, {year: '2014-2015', term: '1'}, {year: '2013-2014', term: '2'} ] var unique = {}; obj.forEach(function(gpa) { unique[JSON.stringify(gpa)] = gpa }); obj = Object.keys(unique).map(function(u) { return JSON.parse(u) }); console.log(unique) </script> </body> </html>
运行结果:
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码运行效果。
JS实现的对象去重功能示例
- Author -
longzhoufeng声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@