2 回答
TA贡献1796条经验 获得超7个赞
你可以使用object.values()和forEach
select=document.getElementById("select")
obj={0:{id: 75, table_no: 40, capacity: 4, shape: "circle", time: null}, 1:{id: 76, table_no: 41, capacity: 4, shape: "circle", time: null}, 2:{id: 77, table_no: 44, capacity: 4, shape: "circle", time: null}, 3:{id: 78, table_no: 45, capacity: 4, shape: "circle", time: null}, 4:{id: 79, table_no: 42, capacity: 6, shape: "large_rectangle", time: null}, 5:{id: 80, table_no: 43, capacity: 6, shape: "large_rectangle", time: null,}}
Object.values(obj).forEach(o=>{
var option = document.createElement("option")
option.text= o.table_no
option.value = o.id
select.appendChild(option)})
<select id="select">
</select>
TA贡献1818条经验 获得超8个赞
您可以更改 PHP 以返回一个对象数组:
while($stmt->fetch()){
$tables[$id]['id'] = $id;
$tables[$id]['table_no'] = $number;
$tables[$id]['capacity'] = $capacity;
$tables[$id]['shape'] = $shape;
$tables[$id]['time'] = $time;
$tables[$id]['joinable'] = $joinable;
$tables[$id]['area'] = $area;
$tables[$id]['baby_friendly'] = $babyFriendly;
$tables[$id]['premise_code'] = $premiseCode;
$tables[$id]['area_name'] = $areaName;
$tables[$id]['premise_name'] = $premiseName;
}
成为
$tables = $stmt->fetchAll(PDO::FETCH_ASSOC);
(但是,您的字段名称会略有不同,如果它们需要相同,请使用 AS 为查询中的字段名称添加别名)。
现在您可以在生成的 javascript 数组上自由使用 map 了。
添加回答
举报