javascript - Accessing the properties of an object without knowing the names of those properties not working -
i have index.php with:
these php lines
$sql = "select * voedingsmiddelen"; $result = mysqli_query($conn, $sql);
on same page have code extract data table , put in array.
var array = <?php echo json_encode( mysqli_fetch_all( $result, mysqli_assoc ) ) ?>; var arraytype = typeof array; alert('type: '+arraytype); var i=0; for(key in array) { alert('for loop working'); if(data.hasownproperty(key)) { var value = data[key]; alert(value); } else { alert('has no property'); } }
i 1 alert saying 'type: object'. "gettype" working. other alerts not displaying. doing wrong?
hasownproperty
returns boolean value indicating whether object on calling has property name of argument here.so try this
for (var i=0; i<array.length; i++) { alert('for loop working'); if(array.hasownproperty(i)) { var value = i; alert(value); } else { alert('has no property'); } }
or in case this:
var i=0; for(key in array) { alert('for loop working'); if(array.hasownproperty(key)) { var value = key; alert(value); } else { alert('has no property'); } }
Comments
Post a Comment