Mahesh Cheema’s Post

flat vs flatMap flat: 1* Used to "flatten" nested array. 2* By default, .flat() only goes one level deep. Using Infinity it goes to all levels 3* flat(depth) flatMap: 1*  It is a combination of .map() followed by .flat(1). 2* It’s more efficient than doing a map and a flat separately because it only iterates through the list once. 3* flatMap(callback) const flatFunc = (list) => {   return list.flat(Infinity) }; console.log(flatFunc([1,2,3,[4,[5,[6]]]])); // [ 1, 2, 3, 4, 5, 6 ] const flatMapFunc = (list) => {   return list.flatMap((item) => item.split(" ")) } console.log(flatMapFunc(["Hello JavaScript", "Hey TypeScript"])); //[ 'Hello', 'JavaScript', 'Hey', 'TypeScript' ] #JavaScript #WebDev #CodingTips #Frontend #SoftwareEngineering #LearnToCode #JSShorts #Programming #100DaysOfCode #CleanCode #WebDevelopment #JavaScriptDeveloper #TechTips #MaheshCheema

  • text

To view or add a comment, sign in

Explore content categories