Reputation: 1192
I have an object and I need to go through each theCtns.services.depends_on
and test each value of this property. If the value is equal to my target value I want to remove it.
var theCtns = {
"version": "2",
"networks": {
"default": {
"ipam": {
"config": [
{
"subnet": "0.0.0.0/26",
"gateway": "0.0.0.0"
}
]
}
}
},
"services": {
"serviceOne": {
"image": "img1",
"container_name": "serviceOneName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceTwo",
"serviceX"
]
},
"serviceTwo": {
"image": "img2",
"container_name": "serviceTwoName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceX"
]
}
}
}
for (var i= theCtns.services.length-1; i>=0; i--){
for (var j= theCtns.services[i].depends_on.length-1; j>=0; j--){
if (theCtns.services[i].depends_on[j] === "serviceTwo") {
theCtns.services[i].depends_on.splice(j, 1);
}
}
//theCtns.services[i].depends_on = theCtns.services[i].depends_on.filter(x=>x!=="serviceTwo")
}
console.log(theCtns)
in reality this object can have more than 8 services but 2 are suffisent to solve my problem
So my loop should go in each service
then each depends_on
and test the value but it looks like my loop is wrong somewhere
[EDIT] corrected the lenght
typo
Upvotes: 1
Views: 41
Reputation: 7253
Check the modified code, since it's not an array but an object, you need to use for...var. Also, you can directly make use of array methods to find the target value in the depends_on field.
var theCtns = {
"version": "2",
"networks": {
"default": {
"ipam": {
"config": [
{
"subnet": "0.0.0.0/26",
"gateway": "0.0.0.0"
}
]
}
}
},
"services": {
"serviceOne": {
"image": "img1",
"container_name": "serviceOneName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceTwo",
"serviceX"
]
},
"serviceTwo": {
"image": "img2",
"container_name": "serviceTwoName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceX"
]
}
}
}
for (var idx in theCtns.services) {
var deps_idx =
theCtns.services[idx].depends_on.indexOf('serviceTwo');
if (deps_idx) {
theCtns.services[idx].depends_on.splice(idx, 1);
}
}
console.log(theCtns.services);
Upvotes: 1
Reputation: 3655
theCtns.services
is not an Array
and you are trying to iterate through it as if it was one. It is actually an Object
.
An Object
has no length, that is why your loop was going wrong. Also theCtns.services[i]
was wrong because it was referring to values such as theCtns.services[0]
that do not exist.
I changed your code so that i
takes its values from the actual keys of your object.
var theCtns = {
"version": "2",
"networks": {
"default": {
"ipam": {
"config": [
{
"subnet": "0.0.0.0/26",
"gateway": "0.0.0.0"
}
]
}
}
},
"services": {
"serviceOne": {
"image": "img1",
"container_name": "serviceOneName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceTwo",
"serviceX"
]
},
"serviceTwo": {
"image": "img2",
"container_name": "serviceTwoName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceX"
]
}
}
}
for (var i in theCtns.services){
console.log(i);
for (var j= theCtns.services[i].depends_on.length-1; j>=0; j--){
if (theCtns.services[i].depends_on[j] === "serviceTwo") {
theCtns.services[i].depends_on.splice(j, 1);
}
}
}
console.log(theCtns)
Upvotes: 1
Reputation: 64526
The issue is theCtns.services
is an object and you are trying to use an incremental loop to iterate it like an array. You need to iterate the keys of the object instead using a for...in
loop.
var theCtns = {
"version": "2",
"networks": {
"default": {
"ipam": {
"config": [
{
"subnet": "0.0.0.0/26",
"gateway": "0.0.0.0"
}
]
}
}
},
"services": {
"serviceOne": {
"image": "img1",
"container_name": "serviceOneName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceTwo",
"serviceX"
]
},
"serviceTwo": {
"image": "img2",
"container_name": "serviceTwoName",
"volumes": [
"x:x"
],
"environment": [
"SOMETHING=bla",
"SOMETHINGELSE=bli"
],
"depends_on": [
"serviceX"
]
}
}
}
for (var i in theCtns.services){
if(theCtns.services.hasOwnProperty(i)){
for (var j= theCtns.services[i].depends_on.length-1; j>=0; j--){
if (theCtns.services[i].depends_on[j] === "serviceTwo") {
theCtns.services[i].depends_on.splice(j, 1);
}
}
}
}
console.log(theCtns)
Upvotes: 3