0

I'm running a function that gets the value of a field name and and sets the desired variable to that field name. The problem is that the variable doesn't return any values and I'm not sure why? There must be something I'm doing wrong but it's just not obvious to me.

 var latField, longField;
 var fieldNames = csvStore.getAttributes(items[0]);

function findSpecificFieldName(originalField, newFieldArray, matchedField){
    var matchId;
    matchId = arrayUtils.indexOf(newFieldArray,
        originalField);
    if (matchId !== -1) {
        matchedField = originalField;
    }
    return matchedField;
}

arrayUtils.forEach(fieldNames, function(fieldName) {
findSpecificFieldName(fieldName, latFieldStrings, latField);
findSpecificFieldName(fieldName, longFieldStrings, longField);
});`

I want the latField variable and longField variable to take the property of the matchedField variable within the function but they don't take it's value. When checking to see if the matchedField variable is equal to the originalField variable it returns the right field but it's not passing that information into the lat and long field variables.

Farris Ismati
  • 176
  • 1
  • 9
  • 1
    Not sure what you're trying to do. You return a value but do nothing with it? Or do you think you can change the value of a parameter and it'll extend to outside of the function? – Dave Newton Jul 30 '15 at 00:37
  • I just want to set the latField and longField variables to what ever the matchedField variable inside the function is. I'll use those variables later on not shown in the code to specify what fields to look at for coordinate data – Farris Ismati Jul 30 '15 at 00:38
  • Just a way to remove redundancy. I can run this code fine without the function but since I do this for multiple fields later on in the code I'd rather confine it into a function. – Farris Ismati Jul 30 '15 at 00:39
  • 2
    Well, that's not how JS works. If you're saying the value is in the array you can update the array value, but you can't update `latField` this way without either (a) an assignment to it, or (b) changing a value within an object. But I might be misunderstanding what you're trying to do. – Dave Newton Jul 30 '15 at 00:42
  • 1
    All you did seems to be changing the copy of latField and longField inside the function. http://stackoverflow.com/questions/7744611/pass-variables-by-reference-in-javascript this might help – Ken Kwok Jul 30 '15 at 00:43

2 Answers2

2

You cannot pass variables by reference in JavaScript.

You should simply assign the return value instead:

latField = findSpecificFieldName(fieldName, latFieldStrings);
longField = findSpecificFieldName(fieldName, longFieldStrings);

You could pass an object as the 3rd parameter, in which case the method will be able to modify its properties, but this is unidiomatic and the wrong way to go about communicating a return value from a function in JavaScript.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thanks I got it to work like that. I also had to change my function to this. `function findSpecificFieldName(newFieldArray){ var newFieldName arrayUtils.forEach(fieldNames, function(fieldName) { matchId = arrayUtils.indexOf(newFieldArray, fieldName); if (matchId !== -1) { newFieldName = fieldName; } }); return newFieldName }` Can't seem to figure out how to format this properly on stackoverflow – Farris Ismati Jul 30 '15 at 01:12
0

Seems that you are lacking the assignment

arrayUtils.forEach(fieldNames, function(fieldName) {
    latField = findSpecificFieldName(fieldName, latFieldStrings, latField);
    longField = findSpecificFieldName(fieldName, longFieldStrings, longField);
});
Leonid Usov
  • 1,508
  • 12
  • 16