0

Hey guys sorry for your time but I have came across to an odd situation. I have a database where I store coordinates (php & mysql) with names. When someone searches for something a php script runs and save the query in 3 different variables which it should be of an array type. Then I use JavaScript to convert these php arrays to JavaScript ones. The coordinates are ok, but the names are of type string when I try split() function it gives error because it is an array (as I understand after searching). The other odd is when I console.log the typeof name it says string but when I look in sources it is displayed like an array.

var lon = <?php echo '["' . implode('", "', $lon) . '"]' ?>;
var lat = <?php echo '["' . implode('", "', $lat) . '"]' ?>;
var name = <?php echo '["' . implode('", "', $name) . '"]' ?>;

the output in sources tab (chrome)

 var lon = ["39.070200", "39.072365", "39.064469"];
 var lat = ["21.018064", "21.017744", "21.000059"];
 var name = ["Saint Dimitrios", "Saint Charalampos", "Saint Kosmas"];

and then for the console

console.log(lon);
console.log(typeof lon);
console.log( typeof name);

the actual output in console

["21.018064", "21.017744", "21.000059"] //which when I click it identifies it as an array
object
string

the declaration in php script

$lon = [];
$lat = [];
$name = [];

and the fetching

while ($row = mysqli_fetch_array($query)) {
        $name[] = $row['name'];
        $lon[] = $row['lon'];
        $lat[] = $row['lat'];
}
Nikolaos
  • 1
  • 2
  • As the post @Xufox refers to, name is a reserved keyword in the window scope and hence it's typeof will return String. The may confusion occur because javascript data types are dynamic – Mathias W Feb 03 '17 at 22:46
  • @MathiasW `name` is _not_ a reserved keyword. It’s a property that has a getter–setter-like behavior. When set, it is cast to string. This doesn’t really relate to dynamic typing. `window.name` just has a special purpose and needs to remain a string. – Sebastian Simon Feb 03 '17 at 23:08
  • Thanks for the correction @Xufox, my bad! – Mathias W Feb 03 '17 at 23:10
  • Yew using the variable name doesn't work! when I changed it as @Xufox suggested it works fine. Sorry for your time and thank you very much! – Nikolaos Feb 04 '17 at 08:35

1 Answers1

-1

In JavaScript arrays are objects with keys as numbers.

Try to create in console arr = new Array and then call typeof(arr) to see your newly created array in vanilla JavaScript environement is an object too.

Beware! When creating new array in JavaScript length property is automatically added to it. So if you try to log every element in array with foreach loop you'll get one more property that is reserved for length of your array.

arr = [1,2,3]
arr.forEach( function(key,i,arr) {
  console.log(key)
})

//output
   1
   2
   3
   undefined

Trying to split() an array is not an option as this function takes string value and breaks it down to array with defined divider ex.

'1,2,3'.split(',')
//result
["1", "2", "3"]

P.S. Functions in JS are objects too. So basically you can make something like this:

function myFunc() {
 alert('Hello World');
};

myFunc.myProperty = '123';

And now you can call your function as usual with myFunc() and also get it's property with console.log(myFunc.myProperty)

Andrew
  • 449
  • 3
  • 7