- Learning Node.js Development
- Andrew Mead
- 513字
- 2021-06-30 18:56:51
Using _.uniq
Back inside the browser, we can use command + F again to search for a new utility, which is _.uniq:

This unique method, simply takes an array and it returns that array with all duplicates removed. That means if I have the same number a few times or the same string, it'll remove any duplicates. Let's run this.
Back inside Atom, we can add this utility into our project, we'll comment out our _.isString calls and we will make a variable called filteredArray. This will be the array without the duplicates, and what we'll do is call, after the equal sign, _.uniq.
Now, as we know, this takes an array. And since we're trying to use the unique function, we'll pass in an array with some duplicates. Use your name twice as a string; I'll use my name once, followed by the number 1, followed by my name again. Then I can use 1, 2, 3, and 4 as shown here:
console.log('Starting app.js');
const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');
// console.log(_.isString(true));
// console.log(_.isString('Gary'));
var filteredArray = _.uniq(['Gary', 1, 'Gary', 1, 2, 3, 4]);
console.log();
// console.log('Result:', notes.add(9, -2));
// var user = os.userInfo();
//
// fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);
Now, if things go as planned, we should get an array with all the duplicates removed, which means we'll have one instance of Gary, one instance of 1, and then 2, 3, and 4, which don't have duplicates.
The last thing to do is to print that using console.log so we can view it inside the Terminal. I'll pass in this filteredArray variable to our console.log statement as shown in the following code:
console.log('Starting app.js');
const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');
// console.log(_.isString(true));
// console.log(_.isString('Gary'));
var filteredArray = _.uniq(['Gary', 1, 'Gary', 1, 2, 3, 4]);
console.log(filteredArray);
// console.log('Result:', notes.add(9, -2));
// var user = os.userInfo();
//
// fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);
From here, we can run our project inside Node. I'll use the last command, then I can press the enter key, and you can see we get our array with all duplicates removed, as shown in the following code output:

We have one instance of the string Gary, one instance of the number 1, and then we have 2, 3, 4, exactly what we expected.
The lodash utility really is endless. There are so many functions that it can be kind of overwhelming to explore at first, but as you start creating more JavaScript and Node projects, you'll find yourself solving a lot of the same problems over and over again when it comes to sorting, filtering, or type checking, and in that case, it's best to use a utility such as lodash to get that lifting done. The lodash utility is great for the following reasons:
- You don't have to keep rewriting your methods
- It is well tested and it has been tried in production
If there were any issues, they've been sorted out by now.
- 黑客攻防實(shí)戰(zhàn)技術(shù)完全手冊:掃描、嗅探、入侵與防御
- 連接未來:從古登堡到谷歌的網(wǎng)絡(luò)革命
- 光網(wǎng)絡(luò)評估及案例分析
- Spring Boot 2.0 Projects
- TCP/IP入門經(jīng)典(第5版)
- 物聯(lián)網(wǎng)與無線傳感器網(wǎng)絡(luò)
- 電力物聯(lián)網(wǎng)工程技術(shù)原理與應(yīng)用
- Spring 5.0 Projects
- Windows Server 2012 Hyper-V虛擬化管理實(shí)踐
- 基于性能的保障理論與方法
- 4G小基站系統(tǒng)原理、組網(wǎng)及應(yīng)用
- Practical Web Penetration Testing
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)
- 精通SEO:100%網(wǎng)站流量提升密碼
- 物聯(lián)網(wǎng)概論