- 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.
- 黑客攻防實戰技術完全手冊:掃描、嗅探、入侵與防御
- 自動駕駛網絡:自智時代的網絡架構
- 智能網聯汽車V2X與智能網聯設施I2X
- Spring Cloud微服務架構進階
- React:Cross-Platform Application Development with React Native
- Learning Swift(Second Edition)
- WordPress Web Application Development
- 6G無線網絡空口關鍵技術
- 人人都該都懂的互聯網思維
- 物聯網頂層設計與關鍵技術
- 網絡空間全球治理觀察
- 園區網絡架構與技術
- Selenium WebDriver 3 Practical Guide
- 圖解物聯網
- 現場綜合化網絡運營與維護:運營商數字化轉型技術與實踐