'My array inputs will not stay when I push a value (Javascript)
Good day everyone, I have some troubles with pushing a value into an empty array. Currently, I want to push a value (subLayerLength); there is only one value that meets this condition of ( > 0) that I want to push into the empty array (subLayersArray) and subsequently use the value that is ( > 0) in the destroyLayerTrNode function removedLayerNode2. However, whenever I run this the array keeps resetting to an empty array. Can anyone tell me how to fix this? Thank you :)
var subLayerLength = layerInfo.newSubLayers.length
let subLayersArray = []
if(subLayerLength > 0){
subLayersArray.push(subLayerLength)}
this.destroyLayerTrNode(layerInfo, subLayersArray)
destroyLayerTrNode: function(layerInfo, subLayersArray) {
var lc = layerInfo.layerObject.url.toLowerCase();
var parentLayerId= layerInfo.id
var layerId = parentLayerId.split("_")[0]
var newSubId= (parseInt(layerInfo.subId))
if (lc.indexOf("abc") > 0 && lc.includes("def")){
for(let i = 1; i< newSubId; i++ ){
var removedLayerNode = query("[class~='layer-tr-node-" + layerId + "_" + i + "']", this.domNode)[0];
var removedLayerContentNode = query("[layercontenttrnodeid='" + layerId + "_" + i + "']", this.domNode)[0];
var removedLayerNode2 = query("[class~='layer-tr-node-" + layerId + "_" + **subLayersArray[0]** + "']", this.domNode)[0];
if(removedLayerNode) {
domConstruct.destroy(removedLayerNode)
domConstruct.destroy(removedLayerNode2)
if(removedLayerContentNode) {
domConstruct.destroy(removedLayerContentNode);
console.log("destroyed")}
}
}
}
},
Solution 1:[1]
The Problem does not seem to be in your code. Pushing the value into the array works fine.
var subLayerLength = 33;
let subLayersArray = [];
if(subLayerLength > 0){
subLayersArray.push(subLayerLength);
}
console.log(subLayersArray);
Your problem seems to be that you donĀ“t use "subLayersArray" in your function "destroyLayerTrNode". You only access an array with the name "subArrayLayer[0]" which is not defined anywhere. Just renaming it should do the trick
var removedLayerNode2 = query("[class~='layer-tr-node-" + layerId + "_" + **subLayersArray[0]** + "']", this.domNode)[0];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | GJohannes |