Skip to main content

My last day of work and hoping it's a beginning of something new

It was my last day of work and I'm happy I finished my remaining tasks on time. And now, it's time for me to move on to my next journey. The only reason I took the job was for me to get enough experience to build my own app. So I already planned it that I will quit my job after 3 years since I signed the 3-year bond contract.  Didn't know that it was tough having one source of income but I think I'm good since I'm still living with my parents... There are times that I compare my income and life to my coworkers, same age brackets and shit... which resulted for me to lose my focus and my mind shifted a lot of times, and I think it's a waste of time to repeat it again. But... at the end of the day... I'm just too lazy I guess and I think that a day job is not suited for me. I'm more of the build one app and make it rain type of guy... Don't even know what to do now!!! I guess I'll try playing stocks and multiplying my money while trying to build my ...

Lodash - The best 3rd party library for react and javascript developers

Hello again my dear noobies,

After some years of using the React framework, I finally feel like I need to say this especially to those newbies out there in the javascript language or React framework.

Tired with those messy looking codes while looping on an array? Or maybe you want to get the sum of the list? Or maybe you want to convert the list of object into a list of components?

You may have lots of problems and spaghetti codes but worry no more.

The lodash library is here to save your day.

Loops

Before anything else let's start with loops, afterall, everyone else uses loops here.

For example we have an array named data where it's value is a list of object with properties name and age.

const data = [
    {"name": "Zack", age: 36},
    {"name": "Ashley": age: 12}
]

and imagine we want to print all name...

Codes like these:
for (let i = 0; i < data.length; i++) {
    let d = data[i];
    console.log(d.name);
}

Can be converted to codes like these:
_.each(data, (d) => {
    console.log(d.name);
});

It's just one line less of code. But guess what, you'll love it in the future since it offers an additional null checker. Even if the data is null it will still not throw an error unlike the code above. The less code, the cleaner, the better.

Maps

But what if we want to change the array of data and return only an array of names? 

Of course we can always use loops
let names = [];
for (let i = 0; i < data.length; i++) {
    let d = data[i];
    names.push(d.name);
}
console.log(names); //will print: ["Zack","Ashley"]

But with maps we can do it faster
console.log(data.map(d => d.name));  //will print: ["Zack","Ashley"]

But what if data is null then you'll get an error so the best way and the least code is this:
console.log(_.map(data, d => d.name)); //will print: ["Zack","Ashley"]

As you can see by using lodash we have less code and more functionality (since there is a null checker built in).

And...

Here comes the best part for react developers. Imagine you have a component called IdCard that accepts two properties age and name. And, you also want to make this one dynamic so that the web app changes based on the data array. What will you do?

Since you are a noobie programmer you will surely do this:
render() {
    const { data } = this.props;
    let idCards = [];
    for (let i = 0; i < data.length; i++) {
        let d = data[i];
        idCards.push(<IdCard name={d.name} age={d.age} />);
    }
    return (
        <div className='id-card-list'>
            {arr}
        </div>
    );
}

booo!!! I'm not saying it's wrong but there's a much cleaner and faster way.

And here it is:
render() {
    const { data } = this.props;
    return (
        <div className='id-card-list'>
            {_.map(data, d => {
                return <IdCard name={d.name} age={d.age} />
            })}
        </div>
    );
}

You don't have to initialize an array (which saves memory) and it's less code.

Reduce

Sometimes you just might wanna get the total age of a list of person...

A noobie programmer will do this:
let totalAge = 0;
for (let i = 0; i < data.length; i++) {
    let d = data[i];
    totalAge += d.age;
}

The better way is this:
let totalAge = _.reduce(data, function(sum, dAge) { return sum + dAge; }, 0);

See, from 5 lines now it is reduced to 1 line and that's a real magic. You might be wondering why there is a zero. The zero there is the initial value of the sum, it's like:
let totalAge = 0;

And with that... I hope you started to love lodash. Also, you are beginning to be more like a pro and less like a noobie. Hope you like my content.

If there's a way to do it better...
find it. - Thomas A. Edison




Comments

Popular posts from this blog

Flutter: How to create an error alert or popup notification with SnackBar

There are 3rd party libraries out there that offer alerts or popups in Flutter. But, what I'll be teaching you today is how to use the built in SnackBar widget to produce an error popup or notification easily. For the video version of the tutorial go here:  How to use snackbar in flutter as error alert or popup notification To use the SnackBar widget and make a nice Snack Bar that you see in most google apps like youtube, all you need to do is wrap your entire app with the Scaffold Widget. So let's make create a new file page_wrapper.dart and create a new Widget named PageWrapper that accepts a widget as children. After that all we need to do is call the PageWrapper widget in all of our pages so that we have a base widget that uses the Scaffold in every page. This means we can now use the SnackBar on every page. In the main.dart all you need to do is this: Then now we can create the home page and create a snackbar. Before that we need a trigger ...

What is the DOM and how does it work?

     I've been asked to teach about the DOM... but first and foremost what is the DOM ? Is it when you are angry and you say DOMmit!!! hehe Or Is it your friend DOMinick??? Well kidding aside, the DOM just stands for Document Object Model (đŸ˜…Sorry for joking but please continue reading the article - there are javascript examples below in codepen ). Browser is taking the HTML sent from the server and converting it to the DOM. so the browser is based on the DOM and not on the html and everytime we edit the dom using javascript it gets reflected on the screen. Is the browser's internal programmatic representation of the web page. Which basically means one thing, you can manipulate the web page using the dom. and how do we do it? of course by using our favorite language, JAVASCRIPT! Since the dom is more or less an object in javascript, it contains several function to access, modify and delete certain elements. If you are familiar with jquery a...

My last day of work and hoping it's a beginning of something new

It was my last day of work and I'm happy I finished my remaining tasks on time. And now, it's time for me to move on to my next journey. The only reason I took the job was for me to get enough experience to build my own app. So I already planned it that I will quit my job after 3 years since I signed the 3-year bond contract.  Didn't know that it was tough having one source of income but I think I'm good since I'm still living with my parents... There are times that I compare my income and life to my coworkers, same age brackets and shit... which resulted for me to lose my focus and my mind shifted a lot of times, and I think it's a waste of time to repeat it again. But... at the end of the day... I'm just too lazy I guess and I think that a day job is not suited for me. I'm more of the build one app and make it rain type of guy... Don't even know what to do now!!! I guess I'll try playing stocks and multiplying my money while trying to build my ...