How to Create Flat Version of Deeply Nested Array? | Frontend Interview Questions | Devtools Tech

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

Комментарии • 33

  • @vaideshshankar9899
    @vaideshshankar9899 3 года назад +3

    My solution without using nested function in flatten by utilizing prototype method defined for array.
    function flatten(){
    const arr=[];
    this.map(x=>{
    if(Array.isArray(x)) arr.push(...x.flatten());
    else arr.push(x);
    })
    return arr;
    }
    Array.prototype.flatten=flatten;
    Great explaination btw. Keep posting such awesome content.

    • @DevtoolsTech
      @DevtoolsTech  3 года назад

      Nice. Clean approach. Thanks for sharing!
      Only point would be that this approach would be a tad bit slower than mentioned in the video as on every iteration (array elements), we would be creating a new array and spreading the output. Hence, more operations to perform. Still a good solution. :D

    • @vaideshshankar9899
      @vaideshshankar9899 3 года назад

      @@DevtoolsTech yeah that's a catch. Thanks for pointing this out.

  • @subhabratadutta3408
    @subhabratadutta3408 2 года назад +3

    Ans for the follow up question (flatten to a certain deep ) :- function flatten(deep) {
    // write your code here
    let output = [];
    function processing(arr,deep) {
    for(let i = 0; i < arr.length ; i++ ) {
    const current = arr[i];
    if(Array.isArray(current) && deep > 0) {
    processing(current,deep - 1);
    } else {
    output.push(current);
    }
    }
    }

    processing(this,deep);
    return output;
    }
    Array.prototype.flatten = flatten;

  • @poonamchaurasia2484
    @poonamchaurasia2484 2 года назад

    really helpful, Great Content !
    Thanks a lot

  • @jithendra16
    @jithendra16 3 года назад

    I never thought of the 2nd solution, really impressive! Great Content !

    • @DevtoolsTech
      @DevtoolsTech  3 года назад +1

      Thank you. Glad you liked it. 😄

  • @DilipKumar-dv7zd
    @DilipKumar-dv7zd 3 года назад

    As always , i again fall in love with your explanation , people who are missing thia channel is loosing out so much, i am eagerly waiting to connect with you yomesh bhaiya

  • @charuhas1602
    @charuhas1602 2 года назад

    Thanks ... More such problems bro .. going great

  • @PIYUSH-lz1zq
    @PIYUSH-lz1zq 2 года назад +1

    sir , can u make videos to make nested comment section and folder structure using JS

  • @bhatvikrant
    @bhatvikrant 2 года назад

    My answer for flattening with depth:
    Array.prototype.flattenDepth = function (depth) {
    let flatArray = [];
    for (let i = 0; i < this.length; i++) {
    if (depth && Array.isArray(this[i])) {
    const returned = this[i].flattenDepth(depth - 1);
    flatArray = flatArray.concat(returned);
    } else {
    flatArray.push(this[i]);
    }
    }
    return flatArray;
    }

  • @Abhishek-dp5tc
    @Abhishek-dp5tc 3 года назад +1

    You can create a repo create a readme or file for every question and we can submit our solution for that question?
    How is the idea?

    • @DevtoolsTech
      @DevtoolsTech  3 года назад

      Sounds like a good idea. Will look into this! :D

  • @naveenkaushik4859
    @naveenkaushik4859 3 года назад

    Hi Yomesh,
    Thanks for explaining it. Can you make a detailed video on - Time ago functionality in javascript.
    I mean how diff ways and in min lines in javascript we can make this functionality.

  • @krishnachaitanya8088
    @krishnachaitanya8088 3 года назад

    Thanks this was asked in one good product base company :(

  • @mohammedyaseenchauhan5551
    @mohammedyaseenchauhan5551 3 года назад

    Thanks! Great explanation👍

  • @MrAagamdoshi
    @MrAagamdoshi 3 года назад

    Got the same question for one online interview for Front end round

    • @DevtoolsTech
      @DevtoolsTech  3 года назад

      ohh great! Do share your solution with us all! :D

  • @megheshshenoy
    @megheshshenoy 3 года назад

    Hi Yomesh!Can you please give an example for the depth Search?

    • @DevtoolsTech
      @DevtoolsTech  3 года назад

      Hi, thank you for your comment. Do you mean Depth-first search? Like traversing a tree?

    • @megheshshenoy
      @megheshshenoy 3 года назад

      @@DevtoolsTech No Like You Told RIght In the Video if we pass a parameter to flatten indicating depth .As Told in Video 16:22

    • @ramji3738
      @ramji3738 3 года назад

      @@megheshshenoy By depth he meant the position of the element in the given array. for ex: the first three elements 1,2,3 are i just in input array so its depth is 0 but element 4 is inside another array so its depth is 1 and so on depth of element is based on where the element is positioned from the given array

  • @ramji3738
    @ramji3738 3 года назад

    Good content brother .. I have a question here ... the last statement of the problem says it has to be invoked existing and earlier array .. so if you try to invoke the function Input.flatten() before line 78 it will throw an error since the function flatten is not assigned yet ... so we have to use IIFE on the Array.prototype.flatten = flatten right ?

    • @DevtoolsTech
      @DevtoolsTech  3 года назад +1

      Yes, this is more like Polyfill so these get included before the source code. So, that function is assigned before usage.

  • @coder4real
    @coder4real 3 года назад

    Or you could demostrate them your ES2019 skills and make it happen by
    Array.prototype.flatten = function() {
    return this.flat(Infinity)
    }

    • @DevtoolsTech
      @DevtoolsTech  3 года назад

      Yes, ofcourse. Idea was discuss how to implement this and understand different concepts via doing so.

  • @GauravYadav-rv7wx
    @GauravYadav-rv7wx 3 года назад

    Who are others two, in your thumbnail?

    • @DevtoolsTech
      @DevtoolsTech  3 года назад +3

      Devtools Tech comprises of 3 like-minded engineers. Other two are my fellow amazing content creators: Saloni Kathuria and Puneet Ahuja. Check-out and subscribe to our channel for their useful videos!
      ~ Yomesh Gupta

  • @deevoid
    @deevoid 3 года назад +1

    flatter than the flat earth society

  • @bhatvikrant
    @bhatvikrant 2 года назад +1

    How about this as a solution? Would love to know your thoughts Yomesh and others.
    Array.prototype.flatten = function () {
    let flatArray = [];
    for (let i = 0; i < this.length; i++) {
    if (Array.isArray(this[i])) {
    const returned = this[i].flatten();
    flatArray = flatArray.concat(returned);
    } else {
    flatArray.push(this[i]);
    }
    }
    return flatArray;
    }