Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




from() - FUNCTION


from() Function is used to return an Array from any object.


Let us say, we a number 5829 and we want each number as a unique array element, 5, 8, 2 and 9.

JavaScript from()-Function

And from() function helps us achieve the above array.


Let us see in the below example.


Example :



<html>
<body>  
<script>

	var x = "5829"
	var y = Array.from(x)
	
    document.write(y)
	    
</script>      
</body>
</html>


Output :



  5,8,2,9

And we have created an array of individual elements.

JavaScript from()-Function

Just remember to take 5829 as a String.


var x = "5829"

And Array.from() will convert it to an array with four elements.


You can also call a map() if you want to perform some calculations. You can pass it as second argument.


Say you want to check which value is equivalent to 8.


Let us see in the below example.


Example :



<html>
<body>  
<script>

	function numberCheck(i) {
  		return i == 8
	}

	var x = "5829"
	var y = Array.from(x, numberCheck)
	
    document.write(y)
	    
</script>      
</body>
</html>


Output :



  false,true,false,false