1

I realized that when I put a loop inside a function and want to return the value it will give me only first value. Like in this example:

var arr = [[1,2], [3,4], [5,6]];

function getVal(item) {
    var result;
    for (var i = 0; i < item.length; i++) {
        for (var j = 0; j < item[i].length; j++) {
            return (item[i][j])
        }
    }
}

console.log(getVal(arr));

It will give me only the value of 1. But when I put console.log instead of return in function it will give me expected result (1,2,3,4,5,6). What's wrong here?

muszynov
  • 319
  • 5
  • 22
  • The function is performing exactly as expected. What are you wanting to happen? This function will always return the value at arr[0][0], which is 1. If you replace the return with a console.log, then it will loop over every possible value. – Dave Goodchild May 13 '18 at 19:54
  • I'm just not sure if putting `console.log` inside loop is a good practice? What if I want to get whole result, not only the first element? – muszynov May 13 '18 at 19:54
  • 1
    Possible duplicate of [Does return stop a loop?](https://stackoverflow.com/questions/11714503/does-return-stop-a-loop) – GalAbra May 13 '18 at 19:55
  • @icelandico a console.log inside a loop is fine, but best to use only for debugging. You need to accumulate the values in another array if you want to flatten that array. – Dave Goodchild May 13 '18 at 19:55
  • 1
    @icelandico people are adding a lot of solutions that will give you the result you appear to be looking for, but I suspect if you're learning Javascript, so go with the solution you understand at this stage, you'll get to the more complex `(inputArr) => [].concat(...inputArr);` solutions later and as well they don't work in all browsers! – Dave Goodchild May 13 '18 at 19:58

4 Answers4

2

You could collect the values and return at the end of the function instead of an early exit with one value.

function getVal(item) {
    var result = [];
    for (var i = 0; i < item.length; i++) {
        for (var j = 0; j < item[i].length; j++) {
            result.push(item[i][j]);
        }
    }
    return result;
}

var arr = [[1, 2], [3, 4], [5, 6]];

console.log(getVal(arr));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

The return keyword will end the function with the given return value. console.log() just gives the output of the value.

Tyr
  • 2,810
  • 1
  • 12
  • 21
1

this should get u going:

var arr = [[1,2], [3,4], [5,6]];

function getVal(item) {
    var result = [];
    for (var i = 0; i < item.length; i++) {
        for (var j = 0; j < item[i].length; j++) {
            result.push(item[i][j]);
        }
    }
    return result;
}

console.log(getVal(arr));
Ali Yılmaz
  • 1,657
  • 1
  • 11
  • 28
  • That's what I wanted. I missed the part of creating an array. Thank You. – muszynov May 13 '18 at 19:55
  • @icelandico looks like youre missing important conceptual stuff here. I would suggest you to delve deeper into basic concepts like printing, returning so that you become a better developer. Best of luck. – Ali Yılmaz May 13 '18 at 19:56
0

If you want to return a flat mapping of the input, you'll probably be better off avoiding the for loops entirely:

const arr = [[1,2], [3,4], [5,6]];
const getVal = (inputArr) => [].concat(...inputArr);
console.log(getVal(arr));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • this answer is correct, but assumes a lot in terms of the available javascript version and is likely more complicated than the OP can deal with just now. – Dave Goodchild May 13 '18 at 19:59