How can I return a different array?












0















I need to return an array consisting of the largest number from each provided sub-array. So, I've written the following code and it works but I have:
[[25],[48],[21],[-3]] instead of [25,48,21,-3]. Can I change something to get the right answer?






function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
}).map(function() {
return arr[i].splice(1);
})
}
return arr;
}

var result = largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

console.log(result);












share|improve this question

























  • One way is that you can flatten it!

    – choz
    Jan 2 at 13:10


















0















I need to return an array consisting of the largest number from each provided sub-array. So, I've written the following code and it works but I have:
[[25],[48],[21],[-3]] instead of [25,48,21,-3]. Can I change something to get the right answer?






function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
}).map(function() {
return arr[i].splice(1);
})
}
return arr;
}

var result = largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

console.log(result);












share|improve this question

























  • One way is that you can flatten it!

    – choz
    Jan 2 at 13:10
















0












0








0








I need to return an array consisting of the largest number from each provided sub-array. So, I've written the following code and it works but I have:
[[25],[48],[21],[-3]] instead of [25,48,21,-3]. Can I change something to get the right answer?






function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
}).map(function() {
return arr[i].splice(1);
})
}
return arr;
}

var result = largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

console.log(result);












share|improve this question
















I need to return an array consisting of the largest number from each provided sub-array. So, I've written the following code and it works but I have:
[[25],[48],[21],[-3]] instead of [25,48,21,-3]. Can I change something to get the right answer?






function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
}).map(function() {
return arr[i].splice(1);
})
}
return arr;
}

var result = largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

console.log(result);








function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
}).map(function() {
return arr[i].splice(1);
})
}
return arr;
}

var result = largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

console.log(result);





function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
}).map(function() {
return arr[i].splice(1);
})
}
return arr;
}

var result = largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

console.log(result);






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 14:45









Armel

1,206920




1,206920










asked Jan 2 at 13:08









Ilya SolodeevIlya Solodeev

33




33













  • One way is that you can flatten it!

    – choz
    Jan 2 at 13:10





















  • One way is that you can flatten it!

    – choz
    Jan 2 at 13:10



















One way is that you can flatten it!

– choz
Jan 2 at 13:10







One way is that you can flatten it!

– choz
Jan 2 at 13:10














6 Answers
6






active

oldest

votes


















1














Loop and sort, then return the first element of each sub-array with map;



function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].sort(function(a,b) {
return b - a;
});
}
return arr.map(array => array[0]);
}


Better still, do it in one.



function largestOfFour(arr) {
return arr.map(array => array.sort((a,b) => b - a)[0]);
}





share|improve this answer



















  • 1





    Better still, use Math.max instead of sort.

    – str
    Jan 2 at 13:38



















3














You could use map with spread syntax ... to return 1D array as pointed out by @str .






function largestOfFour(arr) {
return arr.map(a => Math.max(...a))
}

const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
const result = largestOfFour(arr);
console.log(result)





You could also use reduce






function largestOfFour(arr) {
return arr.reduce((r, a) => r.concat(Math.max(...a)), )
}

const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
const result = largestOfFour(arr);
console.log(result)








share|improve this answer





















  • 3





    concat is not even needed, arr.map(a => Math.max(...a)) is enough.

    – str
    Jan 2 at 13:15











  • return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

    – freddyrobinson
    Jan 2 at 13:19



















0














get the first character of each array using [0] index like this



function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {

arr[i].sort(function(a,b) {
return b - a;

}).map(function() {
// HERE
return arr[i].splice(1)[0];

})

}
return arr;
}

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);





share|improve this answer































    0














    You could map the first element of the descending sorted elements.






    function largestOfFour(array) {
    return array.map(a => a.sort((a, b) => b - a)[0]);
    }

    console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));








    share|improve this answer































      0














      You can use map which will return a new array and then use Math.max to get the maximum value form the array






      function largestOfFour(arr) {
      return arr.map(item => {
      return [Math.max.apply(null, item)]

      })
      }

      console.log(largestOfFour([
      [17, 23, 25, 12],
      [25, 7, 34, 48],
      [4, -10, 18, 21],
      [-72, -3, -17, -10]
      ]));








      share|improve this answer































        -2














        you can use flat function for it



        var arr1 = [1, 2, [3, 4]];
        arr1.flat();
        // [1, 2, 3, 4]

        var arr2 = [1, 2, [3, 4, [5, 6]]];
        arr2.flat();
        // [1, 2, 3, 4, [5, 6]]

        var arr3 = [1, 2, [3, 4, [5, 6]]];
        arr3.flat(2);
        // [1, 2, 3, 4, 5, 6]





        share|improve this answer



















        • 1





          It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

          – Armel
          Jan 2 at 13:14








        • 1





          Note that this is not supported in IE or Edge

          – freddyrobinson
          Jan 2 at 13:15











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006957%2fhow-can-i-return-a-different-array%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Loop and sort, then return the first element of each sub-array with map;



        function largestOfFour(arr) {
        for (let i = 0; i < arr.length; i++) {
        arr[i].sort(function(a,b) {
        return b - a;
        });
        }
        return arr.map(array => array[0]);
        }


        Better still, do it in one.



        function largestOfFour(arr) {
        return arr.map(array => array.sort((a,b) => b - a)[0]);
        }





        share|improve this answer



















        • 1





          Better still, use Math.max instead of sort.

          – str
          Jan 2 at 13:38
















        1














        Loop and sort, then return the first element of each sub-array with map;



        function largestOfFour(arr) {
        for (let i = 0; i < arr.length; i++) {
        arr[i].sort(function(a,b) {
        return b - a;
        });
        }
        return arr.map(array => array[0]);
        }


        Better still, do it in one.



        function largestOfFour(arr) {
        return arr.map(array => array.sort((a,b) => b - a)[0]);
        }





        share|improve this answer



















        • 1





          Better still, use Math.max instead of sort.

          – str
          Jan 2 at 13:38














        1












        1








        1







        Loop and sort, then return the first element of each sub-array with map;



        function largestOfFour(arr) {
        for (let i = 0; i < arr.length; i++) {
        arr[i].sort(function(a,b) {
        return b - a;
        });
        }
        return arr.map(array => array[0]);
        }


        Better still, do it in one.



        function largestOfFour(arr) {
        return arr.map(array => array.sort((a,b) => b - a)[0]);
        }





        share|improve this answer













        Loop and sort, then return the first element of each sub-array with map;



        function largestOfFour(arr) {
        for (let i = 0; i < arr.length; i++) {
        arr[i].sort(function(a,b) {
        return b - a;
        });
        }
        return arr.map(array => array[0]);
        }


        Better still, do it in one.



        function largestOfFour(arr) {
        return arr.map(array => array.sort((a,b) => b - a)[0]);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 13:14









        prettyflyprettyfly

        1,23611021




        1,23611021








        • 1





          Better still, use Math.max instead of sort.

          – str
          Jan 2 at 13:38














        • 1





          Better still, use Math.max instead of sort.

          – str
          Jan 2 at 13:38








        1




        1





        Better still, use Math.max instead of sort.

        – str
        Jan 2 at 13:38





        Better still, use Math.max instead of sort.

        – str
        Jan 2 at 13:38













        3














        You could use map with spread syntax ... to return 1D array as pointed out by @str .






        function largestOfFour(arr) {
        return arr.map(a => Math.max(...a))
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        You could also use reduce






        function largestOfFour(arr) {
        return arr.reduce((r, a) => r.concat(Math.max(...a)), )
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)








        share|improve this answer





















        • 3





          concat is not even needed, arr.map(a => Math.max(...a)) is enough.

          – str
          Jan 2 at 13:15











        • return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

          – freddyrobinson
          Jan 2 at 13:19
















        3














        You could use map with spread syntax ... to return 1D array as pointed out by @str .






        function largestOfFour(arr) {
        return arr.map(a => Math.max(...a))
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        You could also use reduce






        function largestOfFour(arr) {
        return arr.reduce((r, a) => r.concat(Math.max(...a)), )
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)








        share|improve this answer





















        • 3





          concat is not even needed, arr.map(a => Math.max(...a)) is enough.

          – str
          Jan 2 at 13:15











        • return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

          – freddyrobinson
          Jan 2 at 13:19














        3












        3








        3







        You could use map with spread syntax ... to return 1D array as pointed out by @str .






        function largestOfFour(arr) {
        return arr.map(a => Math.max(...a))
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        You could also use reduce






        function largestOfFour(arr) {
        return arr.reduce((r, a) => r.concat(Math.max(...a)), )
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)








        share|improve this answer















        You could use map with spread syntax ... to return 1D array as pointed out by @str .






        function largestOfFour(arr) {
        return arr.map(a => Math.max(...a))
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        You could also use reduce






        function largestOfFour(arr) {
        return arr.reduce((r, a) => r.concat(Math.max(...a)), )
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)








        function largestOfFour(arr) {
        return arr.map(a => Math.max(...a))
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        function largestOfFour(arr) {
        return arr.map(a => Math.max(...a))
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        function largestOfFour(arr) {
        return arr.reduce((r, a) => r.concat(Math.max(...a)), )
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)





        function largestOfFour(arr) {
        return arr.reduce((r, a) => r.concat(Math.max(...a)), )
        }

        const arr = [[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
        const result = largestOfFour(arr);
        console.log(result)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 13:18

























        answered Jan 2 at 13:13









        Nenad VracarNenad Vracar

        72.8k126083




        72.8k126083








        • 3





          concat is not even needed, arr.map(a => Math.max(...a)) is enough.

          – str
          Jan 2 at 13:15











        • return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

          – freddyrobinson
          Jan 2 at 13:19














        • 3





          concat is not even needed, arr.map(a => Math.max(...a)) is enough.

          – str
          Jan 2 at 13:15











        • return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

          – freddyrobinson
          Jan 2 at 13:19








        3




        3





        concat is not even needed, arr.map(a => Math.max(...a)) is enough.

        – str
        Jan 2 at 13:15





        concat is not even needed, arr.map(a => Math.max(...a)) is enough.

        – str
        Jan 2 at 13:15













        return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

        – freddyrobinson
        Jan 2 at 13:19





        return arr.map(function(a) { return Math.max.apply(Math, a); }); If you need to support IE

        – freddyrobinson
        Jan 2 at 13:19











        0














        get the first character of each array using [0] index like this



        function largestOfFour(arr) {
        for (let i = 0; i < arr.length; i++) {

        arr[i].sort(function(a,b) {
        return b - a;

        }).map(function() {
        // HERE
        return arr[i].splice(1)[0];

        })

        }
        return arr;
        }

        largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);





        share|improve this answer




























          0














          get the first character of each array using [0] index like this



          function largestOfFour(arr) {
          for (let i = 0; i < arr.length; i++) {

          arr[i].sort(function(a,b) {
          return b - a;

          }).map(function() {
          // HERE
          return arr[i].splice(1)[0];

          })

          }
          return arr;
          }

          largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);





          share|improve this answer


























            0












            0








            0







            get the first character of each array using [0] index like this



            function largestOfFour(arr) {
            for (let i = 0; i < arr.length; i++) {

            arr[i].sort(function(a,b) {
            return b - a;

            }).map(function() {
            // HERE
            return arr[i].splice(1)[0];

            })

            }
            return arr;
            }

            largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);





            share|improve this answer













            get the first character of each array using [0] index like this



            function largestOfFour(arr) {
            for (let i = 0; i < arr.length; i++) {

            arr[i].sort(function(a,b) {
            return b - a;

            }).map(function() {
            // HERE
            return arr[i].splice(1)[0];

            })

            }
            return arr;
            }

            largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 13:12









            Joey GoughJoey Gough

            744419




            744419























                0














                You could map the first element of the descending sorted elements.






                function largestOfFour(array) {
                return array.map(a => a.sort((a, b) => b - a)[0]);
                }

                console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));








                share|improve this answer




























                  0














                  You could map the first element of the descending sorted elements.






                  function largestOfFour(array) {
                  return array.map(a => a.sort((a, b) => b - a)[0]);
                  }

                  console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));








                  share|improve this answer


























                    0












                    0








                    0







                    You could map the first element of the descending sorted elements.






                    function largestOfFour(array) {
                    return array.map(a => a.sort((a, b) => b - a)[0]);
                    }

                    console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));








                    share|improve this answer













                    You could map the first element of the descending sorted elements.






                    function largestOfFour(array) {
                    return array.map(a => a.sort((a, b) => b - a)[0]);
                    }

                    console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));








                    function largestOfFour(array) {
                    return array.map(a => a.sort((a, b) => b - a)[0]);
                    }

                    console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));





                    function largestOfFour(array) {
                    return array.map(a => a.sort((a, b) => b - a)[0]);
                    }

                    console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]));






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 13:14









                    Nina ScholzNina Scholz

                    193k15106177




                    193k15106177























                        0














                        You can use map which will return a new array and then use Math.max to get the maximum value form the array






                        function largestOfFour(arr) {
                        return arr.map(item => {
                        return [Math.max.apply(null, item)]

                        })
                        }

                        console.log(largestOfFour([
                        [17, 23, 25, 12],
                        [25, 7, 34, 48],
                        [4, -10, 18, 21],
                        [-72, -3, -17, -10]
                        ]));








                        share|improve this answer




























                          0














                          You can use map which will return a new array and then use Math.max to get the maximum value form the array






                          function largestOfFour(arr) {
                          return arr.map(item => {
                          return [Math.max.apply(null, item)]

                          })
                          }

                          console.log(largestOfFour([
                          [17, 23, 25, 12],
                          [25, 7, 34, 48],
                          [4, -10, 18, 21],
                          [-72, -3, -17, -10]
                          ]));








                          share|improve this answer


























                            0












                            0








                            0







                            You can use map which will return a new array and then use Math.max to get the maximum value form the array






                            function largestOfFour(arr) {
                            return arr.map(item => {
                            return [Math.max.apply(null, item)]

                            })
                            }

                            console.log(largestOfFour([
                            [17, 23, 25, 12],
                            [25, 7, 34, 48],
                            [4, -10, 18, 21],
                            [-72, -3, -17, -10]
                            ]));








                            share|improve this answer













                            You can use map which will return a new array and then use Math.max to get the maximum value form the array






                            function largestOfFour(arr) {
                            return arr.map(item => {
                            return [Math.max.apply(null, item)]

                            })
                            }

                            console.log(largestOfFour([
                            [17, 23, 25, 12],
                            [25, 7, 34, 48],
                            [4, -10, 18, 21],
                            [-72, -3, -17, -10]
                            ]));








                            function largestOfFour(arr) {
                            return arr.map(item => {
                            return [Math.max.apply(null, item)]

                            })
                            }

                            console.log(largestOfFour([
                            [17, 23, 25, 12],
                            [25, 7, 34, 48],
                            [4, -10, 18, 21],
                            [-72, -3, -17, -10]
                            ]));





                            function largestOfFour(arr) {
                            return arr.map(item => {
                            return [Math.max.apply(null, item)]

                            })
                            }

                            console.log(largestOfFour([
                            [17, 23, 25, 12],
                            [25, 7, 34, 48],
                            [4, -10, 18, 21],
                            [-72, -3, -17, -10]
                            ]));






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 13:16









                            brkbrk

                            29.1k32243




                            29.1k32243























                                -2














                                you can use flat function for it



                                var arr1 = [1, 2, [3, 4]];
                                arr1.flat();
                                // [1, 2, 3, 4]

                                var arr2 = [1, 2, [3, 4, [5, 6]]];
                                arr2.flat();
                                // [1, 2, 3, 4, [5, 6]]

                                var arr3 = [1, 2, [3, 4, [5, 6]]];
                                arr3.flat(2);
                                // [1, 2, 3, 4, 5, 6]





                                share|improve this answer



















                                • 1





                                  It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                                  – Armel
                                  Jan 2 at 13:14








                                • 1





                                  Note that this is not supported in IE or Edge

                                  – freddyrobinson
                                  Jan 2 at 13:15
















                                -2














                                you can use flat function for it



                                var arr1 = [1, 2, [3, 4]];
                                arr1.flat();
                                // [1, 2, 3, 4]

                                var arr2 = [1, 2, [3, 4, [5, 6]]];
                                arr2.flat();
                                // [1, 2, 3, 4, [5, 6]]

                                var arr3 = [1, 2, [3, 4, [5, 6]]];
                                arr3.flat(2);
                                // [1, 2, 3, 4, 5, 6]





                                share|improve this answer



















                                • 1





                                  It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                                  – Armel
                                  Jan 2 at 13:14








                                • 1





                                  Note that this is not supported in IE or Edge

                                  – freddyrobinson
                                  Jan 2 at 13:15














                                -2












                                -2








                                -2







                                you can use flat function for it



                                var arr1 = [1, 2, [3, 4]];
                                arr1.flat();
                                // [1, 2, 3, 4]

                                var arr2 = [1, 2, [3, 4, [5, 6]]];
                                arr2.flat();
                                // [1, 2, 3, 4, [5, 6]]

                                var arr3 = [1, 2, [3, 4, [5, 6]]];
                                arr3.flat(2);
                                // [1, 2, 3, 4, 5, 6]





                                share|improve this answer













                                you can use flat function for it



                                var arr1 = [1, 2, [3, 4]];
                                arr1.flat();
                                // [1, 2, 3, 4]

                                var arr2 = [1, 2, [3, 4, [5, 6]]];
                                arr2.flat();
                                // [1, 2, 3, 4, [5, 6]]

                                var arr3 = [1, 2, [3, 4, [5, 6]]];
                                arr3.flat(2);
                                // [1, 2, 3, 4, 5, 6]






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jan 2 at 13:11









                                stackoverflowstackoverflow

                                506




                                506








                                • 1





                                  It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                                  – Armel
                                  Jan 2 at 13:14








                                • 1





                                  Note that this is not supported in IE or Edge

                                  – freddyrobinson
                                  Jan 2 at 13:15














                                • 1





                                  It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                                  – Armel
                                  Jan 2 at 13:14








                                • 1





                                  Note that this is not supported in IE or Edge

                                  – freddyrobinson
                                  Jan 2 at 13:15








                                1




                                1





                                It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                                – Armel
                                Jan 2 at 13:14







                                It's always better to mention the source of our answers: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

                                – Armel
                                Jan 2 at 13:14






                                1




                                1





                                Note that this is not supported in IE or Edge

                                – freddyrobinson
                                Jan 2 at 13:15





                                Note that this is not supported in IE or Edge

                                – freddyrobinson
                                Jan 2 at 13:15


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006957%2fhow-can-i-return-a-different-array%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith