官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 南昌市| 云林县| 宁陵县| 嘉荫县| 农安县| 准格尔旗| 铁岭县| 荃湾区| 蓝田县| 阿克陶县| 荣昌县| 清镇市| 噶尔县| 长春市| 张家港市| 太谷县| 屏东县| 屯留县| 巫山县| 延寿县| 禄丰县| 报价| 邮箱| 汶上县| 赫章县| 财经| 泸西县| 蛟河市| 板桥市| 天津市| 旬阳县| 伊川县| 大竹县| 秭归县| 江安县| 长治县| 拉孜县| 兴义市| 清流县| 兖州市| 剑阁县|