我有一个JavaScript对象数组,如下所示。
$string = "2,3,4,5";
$exp = explode(",",$string);
foreach($exp as $e) {
$query = $conn->query("select * from tbl");
while($row = $query->fetch_assoc()) {
$result .= '<option value="" ';
if($e === $row['id']):
$result .= ' selected';
endif;
$result .= '>';
$result .= $row['id'];
$result .= '</option>';
}
}
我正在尝试添加一个新的二级条目,但还不太清楚。我已经尝试过一些类似的事情:
var satelliteListByGroup = {
Default: {
AccessCX1141016091532: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "1.1.1.1",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
},
AccessCX1180424080022: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "1.1.1.1",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
}
}
};
在这种情况下,推送无效。它似乎只能与主数组名称一起使用。
答案 0 :(得分:2)
push()
是一个数组方法,但是Default
不是一个数组,它是一个对象。您可以使用Object.assign()
:
Object.assign()
方法用于将所有可枚举的自身属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
var satelliteListByGroup = {
Default: {
AccessCX1141016091532: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "1.1.1.1",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
},
AccessCX1180424080022: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "1.1.1.1",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
}
}
};
var arr1 = {
AccessCX11410160916546: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "5.5.5.5",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
}
};
Object.assign(satelliteListByGroup.Default, arr1);
console.log(satelliteListByGroup);
答案 1 :(得分:0)
push不是向对象添加新键的有效语法。在您的情况下,arr1
是一个对象。因此,使用Object.keys
获取键,使用Object.values
从arr1
对象获取键和值。 Object.keys和Object.values都返回一个数组。由于arr1
仅包含一个值,因此您可以使用索引来访问它。
然后使用方括号在Default
对象内部创建密钥
var satelliteListByGroup = {
Default: {
AccessCX1141016091532: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "1.1.1.1",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
},
AccessCX1180424080022: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "1.1.1.1",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
}
}
};
var arr1 = {
AccessCX11410160916546: {
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "5.5.5.5",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
}
};
let getKey = Object.keys(arr1);
let getVals = Object.values(arr1);
satelliteListByGroup.Default[getKey[0]] = getVals[0]
console.log(satelliteListByGroup)
答案 2 :(得分:0)
您正在设置object的属性,而不是在数组中推送元素。 试试这个-
obj ={
'AccessCX11410160916546':{
name: "IAD_ACS4",
iname: "IAD_ACS4_core0",
ra: "[Not assigned]",
ip: "5.5.5.5",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
},
'AccessCX114101890900-':{
name: "IAD_ACS6",
iname: "IAD_ACS4_core2",
ra: "[Not assigned]",
ip: "5.5.5.5",
status: "Permitted, last connected Jan 14, 2019 10:19:12 AM",
col: "#ff5555",
fw: "5.0.0.31",
srv: "Enabled"
},
}
for(let key in obj){
satelliteListByGroup.Default[key]= obj[key];
}