Return values of an array

I have this array:

$data=[95,65,25,12,45,896,325];

I wrote the next code to javascript:

var num=data.length;
   console.log(num);
   console.log(data);
   var arrayValue=function (){
       
       
           for(var i=0;i<=num;i++){
              return data[i];
          }  
           };
           
     console.log(arrayValue()); 

the problem is that it returns only the data[0]=95 value.How i can fix it?I want to post all the present values of the previous array. The console script is this

7

[95, 65, 25, 12, 45, 896, 325]

95

A function will only return one value. You need to rearrange your loop so that each time you loop through, you run the function which returns the value of the array element.

1 Like

Basically i want to pass the values of the myArrary to data: in chart-data.js. Is this the right way?

var lineChartData = {
			labels : ["January","February","March","April","May","June","July"],
			datasets : [
				{
					label: "My First dataset",
					fillColor : "rgba(220,220,220,0.2)",
					strokeColor : "rgba(220,220,220,1)",
					pointColor : "rgba(220,220,220,1)",
					pointStrokeColor : "#fff",
					pointHighlightFill : "#fff",
					pointHighlightStroke : "rgba(220,220,220,1)",
					data : myArray
				},
				{
					label: "My Second dataset",
					fillColor : "rgba(48, 164, 255, 0.2)",
					strokeColor : "rgba(48, 164, 255, 1)",
					pointColor : "rgba(48, 164, 255, 1)",
					pointStrokeColor : "#fff",
					pointHighlightFill : "#fff",
					pointHighlightStroke : "rgba(48, 164, 255, 1)",
					data : myArray
				}
			]

		}

see if this helps

var data = [95, 65, 25, 12, 45, 896, 325];
var num = data.length;
console.log(num);
console.log(data);
var i = 0;
var arrayValue = function() {
  if (i <= num) {
    return (data[i++]);
  }
}
console.log(arrayValue());

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.