<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chiranjit Dey</title>
    <description>The latest articles on DEV Community by Chiranjit Dey (@dey24).</description>
    <link>https://dev.to/dey24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F413504%2Fd139fc8e-2f5b-49ef-b9c9-1c7b126b4e4c.jpeg</url>
      <title>DEV Community: Chiranjit Dey</title>
      <link>https://dev.to/dey24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dey24"/>
    <language>en</language>
    <item>
      <title>Flattening a Deeply Nested Object: A Step-by-Step Guide</title>
      <dc:creator>Chiranjit Dey</dc:creator>
      <pubDate>Tue, 13 Aug 2024 06:37:37 +0000</pubDate>
      <link>https://dev.to/dey24/flattening-a-deeply-nested-object-a-step-by-step-guide-163g</link>
      <guid>https://dev.to/dey24/flattening-a-deeply-nested-object-a-step-by-step-guide-163g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Understanding the Problem&lt;/strong&gt;&lt;br&gt;
Often, we encounter complex data structures in JavaScript applications. These structures can be deeply nested objects, making it challenging to manipulate or process them directly. One common operation is to flatten these objects, transforming them into a simpler structure where all properties are at the top level.&lt;/p&gt;

&lt;p&gt;In this blog, we'll delve into a JavaScript code snippet that effectively flattens a deeply nested object. We'll break down the code line by line, explaining its logic and functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Code Breakdown&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let user = {
  name : 'Chiranjit',
  address : {
    personal : {
      city: 'Kolkata',
      state: 'West Bengal'
    },
    office : {
      city: 'Bengaluru',
      state: 'Karnataka',
      area: {
        landmark:'Waterside',
        post: 433101
      }
    }
  }
}
var finalObj = {} 

const flatObjFn = (obj, parent) =&amp;gt; {
  for(let key in obj){
    if(typeof obj[key] === 'object'){
      flatObjFn(obj[key], parent+'_'+key)
    }else{
      finalObj[parent + '_' + key] = obj[key]
    }
  }
}

flatObjFn(user, 'user');
console.log(finalObj);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Line-by-Line Explanation&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating the Nested Object:

&lt;ul&gt;
&lt;li&gt;We begin by creating a deeply nested object named user. It contains 
properties like name, address, and further nested objects within 
address.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Initializing the Output Object:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An empty object finalObj is created to store the flattened result.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Defining the Flattening Function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function named flatObjFn is defined, accepting two parameters:
a) obj: The object to be flattened.
b) parent: A string to prefix property names for clarity.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Iterating Through Object Properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A for...in loop iterates over each property of the input object obj.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Handling Nested Objects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the value of a property is an object, the flatObjFn function is 
recursively called on that object. The parent parameter is 
concatenated with the current property name to create a new prefix 
for the nested properties.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Handling Primitive Values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the value of a property is not an object (i.e., a primitive value 
like a string or number), it's added to the finalObj with a key 
formed by concatenating the parent and the current property name.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Calling the Flattening Function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The flatObjFn is called with the user object as input and 'user' as 
the initial parent prefix.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Logging the Flattened Object:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The final flattened object is printed to the console using 
console.log(finalObj).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How It Works?&lt;/strong&gt;&lt;br&gt;
The flatObjFn function recursively traverses the object, breaking down nested structures into a flat object. The parent parameter keeps track of the object hierarchy, allowing the function to create meaningful property names in the output object.&lt;/p&gt;

&lt;p&gt;Let's connect on &lt;a href="https://x.com/ChiranjitD2000" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/chiranjit-dey-a198b5188/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Polyfill for Bind()</title>
      <dc:creator>Chiranjit Dey</dc:creator>
      <pubDate>Sun, 11 Aug 2024 06:02:44 +0000</pubDate>
      <link>https://dev.to/dey24/polyfill-for-bind-57p7</link>
      <guid>https://dev.to/dey24/polyfill-for-bind-57p7</guid>
      <description>&lt;p&gt;In the dynamic world of JavaScript development, ensuring cross-browser compatibility is paramount. One essential tool in achieving this is the bind() method. However, older browsers might not fully support it. This is where polyfills come to the rescue.&lt;/p&gt;

&lt;p&gt;A polyfill is essentially a piece of code that provides functionality not natively supported by the older browser. In this case, we'll delve into creating a &lt;strong&gt;polyfill for the bind()&lt;/strong&gt; method to ensure your code works seamlessly across different environments.&lt;/p&gt;

&lt;p&gt;Let's explore how to build this crucial polyfill.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt; : The Bind() method basically allows an object to borrow a method from another object without copying.&lt;/p&gt;

&lt;p&gt;Here is a step by step breakdown, of the Bind polyfill.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrgghk5vubf6qmnktody.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrgghk5vubf6qmnktody.png" alt="Polyfill for Bind"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Ln:1, Person is an object, that has the "name" property and printAge as a function property. PrintAge here prints the name and age.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:8, Person2 is simply another object that has the "name" property but no printAge function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln: 22, In this line, we call the myBind function (which is a polyfill) and store the result in a variable named "bindFunc".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln 12, this is where we write the polyfill for our myBind method. In this function, we take the Object we want to append the myBind method to and other arguments. Since a lot of arguments can be passed, so we basically target all arguments using the "...args". &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next we check if the type of "this", which is the "printAge" function here is a function or not, if not we throw an error, else we assign the function to the obj (person2) as "obj.fn" that is being passed to us as a parameter in our myBind function. But since "bind" method returns a function that gets invoked separately, so here also we return the function with "...args2" as arguments, this is to mean that we can pass extra arguments to it also, that are not supposed to be passed through the polyfill. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln 18, we pass all the arguments to our printAge function. Now it has all that is required and can function just like the bind method. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: Instead of sending the age = 35 in the myBind method invoke (Ln 22), we can pass it as an argument to the bindFunc(35) as well, this is why we are taking the "...args2", to handle extra parameters or arguments&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you like this explanation, let's connect on Twitter or LinkedIn and if you have any feedback, let me know in the comments. Would really help.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://x.com/ChiranjitD2000" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/chiranjit-dey-a198b5188/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Polyfill for Apply()</title>
      <dc:creator>Chiranjit Dey</dc:creator>
      <pubDate>Fri, 09 Aug 2024 06:48:23 +0000</pubDate>
      <link>https://dev.to/dey24/polyfill-for-apply-4o5p</link>
      <guid>https://dev.to/dey24/polyfill-for-apply-4o5p</guid>
      <description>&lt;p&gt;Polyfills are essential tools for developers aiming to create web applications compatible with a wide range of browsers. These code snippets bridge the gap between modern functionalities and older browser limitations.&lt;/p&gt;

&lt;p&gt;Today, we'll delve into the apply() function and explore how to implement a polyfill for it, ensuring your applications function seamlessly across different browser environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given below is a line by line explanation of the code.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The apply() method in JS invokes a function with a specified "this" value and allows passing arguments in an array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:1, person is an object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:5, printAge is a function, that takes age as an arg. (Apply method can take N number of arguments in the array).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzzwq7j1k1fuk0rmblymh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzzwq7j1k1fuk0rmblymh.png" alt="Polyfill for Apply" width="717" height="715"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Ln:9, Function.prototype.myApply is our polyfill that handles the apply method. It takes the object and [arguments].&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:11, we check whether the typeOf "this" and typeOf the ...args, if both satisfies then, &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:10, we make a key in the object naming it fn and assigning "this" to it. "this" refers to the printAge function() here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:19 we call the function with the arguments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:22, we call the myApply method here and get to see our output in the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you like the post, let's connect on Twitter or LinkedIn. I post almost everyday about JS fundamentals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://x.com/ChiranjitD2000" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/chiranjit-dey-a198b5188/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Polyfill for Call()</title>
      <dc:creator>Chiranjit Dey</dc:creator>
      <pubDate>Mon, 05 Aug 2024 06:30:40 +0000</pubDate>
      <link>https://dev.to/dey24/polyfill-for-call-2fed</link>
      <guid>https://dev.to/dey24/polyfill-for-call-2fed</guid>
      <description>&lt;p&gt;Polyfills are essential tools for developers aiming to create web applications compatible with a wide range of browsers. These code snippets bridge the gap between modern functionalities and older browser limitations. &lt;/p&gt;

&lt;p&gt;Today, we'll delve into the call() function and explore how to implement a polyfill for it, ensuring your applications function seamlessly across different browser environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Given below is a line by line explanation of the code.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The call() method in JS invokes a function with a specified "this" value and allows passing arguments individually.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:1, person is an object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:5, printAge is a function, that takes age as an arg. (Call method can take N number of arguments).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0852d1brdrl18lrdiugc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0852d1brdrl18lrdiugc.png" alt="Polyfill for Call" width="707" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4.Ln:9, Function.prototype.myCall is our polyfill that handles the call method. It takes the object and arguments.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Ln:10, we now make a key in the object naming it fn and assigning "this" to it. "this" refers to the printAge function() here. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:11 we call the function with the arguments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ln:14, we call the myCall method here and get to see our output in the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you like the post, make sure to follow me on Twitter and LinkedIn. I post almost everyday about JS fundamentals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://x.com/ChiranjitD2000" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/chiranjit-dey-a198b5188/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
