Notes
  • Introduction
  • 應用程式
    • Azure
      • Logic App
      • web app
    • Android
    • Bower
    • Curl
    • DNS
    • Docker
    • Fail2ban
    • Git
    • GitLab-CI
    • GitLab
    • GPG
    • Home Assistant
    • IIS
    • Line Bot
    • Ngrok
    • Npm
    • PowerShell
    • Redis
    • SSH
    • Synology
    • VS Code
    • Web
  • 程式語言
    • C#
      • 遠端偵錯-Remote Debugger
      • 預設值表
      • .Net Core
    • JavaScript
    • PowerShell
  • 作業系統
    • Mac OS
    • Windows 10
    • Raspberry Pi
    • Ubuntu
  • 其他
    • SSL
    • Tools
Powered by GitBook
On this page
  • Tips
  • Get last word
  • Get duplicated object from array
Edit on Git
  1. 程式語言

JavaScript

Tips

Get last word

var url = 'www.example.com';
console.log(url.split('.').pop());
// com
console.log(url.slice(-3));
// com

Get duplicated object from array

let objArray = [
    { name: '1', id: 0 },
    { name: '2', id: 1 },
    { name: '3', id: 2 },
    { name: '3', id: 3 },
    { name: '3', id: 4 },
    { name: '1', id: 5 },
];

// 所有重複的 obj
// [ { name: '1', id: 0 }, { name: '3', id: 2 }, { name: '3', id: 3 }, { name: '3', id: 4 }, { name: '1', id: 5 } ]
objArray.filter((thing, index, self) =>
  self.findIndex((t, i) => {
    return t.name === thing.name && index2 !== index
  }) > -1
)

// 去除重複的
// [ { name: '1', id: 0 }, { name: '2', id: 1 }, { name: '3', id: 2 } ]
objArray.filter((thing, index, self) =>
  index === self.findIndex((t) => {
    return t.name === thing.name
  })
)

// 取得重複 obj
// [ { name: '3', id: 3 }, { name: '3', id: 4 }, { name: '1', id: 5 } ]
objArray.filter((thing, index, self) =>
  index !== self.findIndex((t) => {
    return t.name === thing.name
  })
)
Previous.Net CoreNextPowerShell

Last updated 5 years ago