admin管理员组文章数量:1405510
How can I push another element to the variables
property from the below object?
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,
}
]
}
For exaple, I need to add {"variable": "var3", "matchType": "Regex", "value": ".*"}
to the variables
property from request
Object...how can I do this?
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": ".*"
});
}
How can I push another element to the variables
property from the below object?
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,
}
]
}
For exaple, I need to add {"variable": "var3", "matchType": "Regex", "value": ".*"}
to the variables
property from request
Object...how can I do this?
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": ".*"
});
}
Share
Improve this question
edited Jun 29, 2016 at 16:17
JAAulde
19.6k5 gold badges56 silver badges64 bronze badges
asked Jun 29, 2016 at 10:28
ValipValip
4,65023 gold badges91 silver badges157 bronze badges
0
5 Answers
Reset to default 3You have to "navigate" properly in your object:
request.rules[0].tags[0].variables.push({
"variable":"var3",
"matchType": "Regex",
"value": ".*"
})
request['variables']
will just try to find the variables
key in root of the request
object. This key is simply not defined but is nested in your object/array structure.
Try like this:
object = {"variable": "var3", "matchType": "Regex", "value": ".*"};
request.rules[0].tags[0].variables.push(object);
request.rules[0].tags.variables[0].push({
"variable":"var3",
"matchType": "Regex",
"value": ".*"
});
You have to navigate in your array. Replace the 0 inside the [] to select the item from the array. (0 is first entry, 1 is second entry etc).
Try:
request.rules[0].tags[0].variables.push({
"variable":"var3",
"matchType": "Regex",
"value": ".*"
})
variables
is into tags
, and tags
is into rules
.
I edited the answer
dot operator(.) can be used to get the value of a particular object property.
square brackets ([]) can be used to access an element of an array.
Now the answer to your question:
request.rules[0].tags[0].variables.push({
"variable": "var3",
"matchType": "Regex",
"value": ".*"
});
here, [0]
specifies the first element of your array.
本文标签: javascriptHow to push data to a nested ObjectStack Overflow
版权声明:本文标题:javascript - How to push data to a nested Object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744921352a2632312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论