如何将另一个元素推送到 variables
来自以下对象的属性?
var request = {
"name": "Name",
"id": 3,
"rules":[
{
"name": "Rule name",
"tags": [
{
"tagId": 1,
"variables":[
{
"variable": "var1",
"matchType": "Regex",
"value": ".*"
},
{
"variable": "var2",
"matchType": "Regex",
"value": ".*"
}
],
"condition": false,
},
{
"tagId": 1,
"condition": false,
}
],
"ruleSetId": 3,
}
]
}
例如,我需要添加{"variable": "var3", "matchType": "Regex", "value": ".*"}
到variables
属性(property)来自 request
对象...我该怎么做?
for(i=0;i<duplicates.length; i++) {
var request = {
"name": duplicates[i].scope,
"id": 3,
"rules":[
{
"name": duplicates[i].scope + " " + "OP SDR Sync",
"tags": [
{
"tagId": 1,
"variables":[
{
}
],
"condition": false,
},
{
"tagId": 1,
"condition": false,
}
],
"ruleSetId": 3,
}
]
}
request.rules[0].tags[0].variables[0].push({
"variable":"var3",
"matchType": "Regex",
"value": ".*"
});
}
最佳答案
您必须在对象中正确“导航”:
request.rules[0].tags[0].variables.push({
"variable":"var3",
"matchType": "Regex",
"value": ".*"
})
request['variables']
将尝试在 request
对象的根中查找 variables
键。这个键根本没有定义,而是嵌套在您的对象/数组结构中。
关于javascript - 如何将数据推送到嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38096937/