Mona Coder
Mona Coder

Reputation: 6316

How to check if dynamic array is empty or not?

How I can check if a Mapped object is empty or not? Basically what I need to do is checking if there is any Array of Fuses in the data? If not, then do something else.

 if(e.attributes.display=='Fuses') {
   if((e.attributes.display == 'Fuses').length == 0){
      console.log("No Data For Fuses");
   }else{
      fuses.push([e.geometry.x,e.geometry.y]);
      }
}

var data = [{
    "displayFieldName": "",
    "fieldAliases": {
      "OBJECTID": "OBJECTID"
    },
    "fields": [{
      "name": "OBJECTID",
      "type": "esriFieldTypeOID",
      "alias": "OBJECTID"
    }],
    "features": [{
        "attributes": {
          "OBJECTID": 649
        }
      },
      {
        "attributes": {
          "OBJECTID": 665
        }
      },
      {
        "attributes": {
          "OBJECTID": 762
        }
      }
    ]
  },
  {
    "displayFieldName": "",
    "fieldAliases": {
      "display": "display",
      "OBJECTID": "OBJECTID"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
    },
    "fields": [{
        "name": "display",
        "type": "esriFieldTypeString",
        "alias": "display",
        "length": 50
      },
      {
        "name": "OBJECTID",
        "type": "esriFieldTypeOID",
        "alias": "OBJECTID"
      }
    ],
    "features": [{
        "attributes": {
          "display": "Transformer",
          "OBJECTID": 1537
        },
        "geometry": {
          "x": -88.17602806699995,
          "y": 41.78431233100008
        }
      },
      {
        "attributes": {
          "display": "Transformer",
          "OBJECTID": 1591
        },
        "geometry": {
          "x": -88.17546081099994,
          "y": 41.783341919000065
        }
      }
    ]
  },
  {
    "displayFieldName": "",
    "fieldAliases": {
      "display": "display",
      "OBJECTID": "OBJECTID"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
      "wkid": 4326,
      "latestWkid": 4326
    },
    "fields": [{
        "name": "display",
        "type": "esriFieldTypeString",
        "alias": "display",
        "length": 50
      },
      {
        "name": "OBJECTID",
        "type": "esriFieldTypeOID",
        "alias": "OBJECTID"
      }
    ],
    "features": [{
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13597
        },
        "geometry": {
          "x": -88.17599727899994,
          "y": 41.78465526100007
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13598
        },
        "geometry": {
          "x": -88.17595382899998,
          "y": 41.78455803400004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13599
        },
        "geometry": {
          "x": -88.17582231499995,
          "y": 41.78435312600004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13600
        },
        "geometry": {
          "x": -88.17561004899994,
          "y": 41.784005335000074
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13601
        },
        "geometry": {
          "x": -88.17557576699994,
          "y": 41.78393182000008
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13602
        },
        "geometry": {
          "x": -88.17535967199996,
          "y": 41.78352876900004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13603
        },
        "geometry": {
          "x": -88.17534426199995,
          "y": 41.78340020400003
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13649
        },
        "geometry": {
          "x": -88.17450698899995,
          "y": 41.78350136200004
        }
      },
      {
        "attributes": {
          "display": "Service Point",
          "OBJECTID": 13650
        },
        "geometry": {
          "x": -88.17435162999999,
          "y": 41.783597986000075
        }
      }
    ]
  }
];

transformers=[];
service_points=[];
fuses=[];

data.forEach(function(el) {
 el.features.forEach(function(e) {
  if(e.attributes.display) {
   if(e.attributes.display=='Transformer') {
      transformers.push([e.geometry.x,e.geometry.y]);
}
  if(e.attributes.display=='Fuses') {
   if((e.attributes.display == 'Fuses').length == 0){
      console.log("No Data For Fuses");
   }else{
      fuses.push([e.geometry.x,e.geometry.y]);
      }
}
  if(e.attributes.display=='Service Point') {
      service_points.push([e.geometry.x,e.geometry.y]);
}
}
});
});

console.log(transformers,service_points, fuses);

Upvotes: 0

Views: 59

Answers (2)

11thdimension
11thdimension

Reputation: 10633

If you need to check the fuses for each data element it can be done as below

data.forEach(function(el) {
  var hasFuses = el.features.some((e) => e.attributes.display === 'Fuses');
  console.log('has: ' + hasFuses);

  el.features.forEach(function(e) {
    if (!hasFuses) {
      console.log("No Data For Fuses");
    }
    switch (e.attributes.display) {
      case 'Transformer':
        transformers.push([e.geometry.x, e.geometry.y]);
        break;
      case 'Fuses':
        fuses.push([e.geometry.x, e.geometry.y]);
        break;
      case 'Service Point':
        service_points.push([e.geometry.x, e.geometry.y]);
        break;
    }
  });
});

console.log(transformers, service_points, fuses);

Upvotes: 1

user184994
user184994

Reputation: 18271

The function below will build up an object of arrays (one for fuses, one for service point, and one for transformer) using reduce, and push the coords in as it goes. When it is done, you can easily check to see if the fuses array .length === 0, and if so, do what you need to

function process() {
    data.forEach((el) => {
        let arrays = el.features.reduce(function(result, feature) {
            if (feature.attributes && feature.attributes.display) {
                // Get the display name to use as the key for our object
                let key = feature.attributes.display.replace(" ", "") 
                // Push the coords into the array
                result[key].push([feature.geometry.x, feature.geometry.y]);
            }
            return result;
        }, {
            // Provide a default object with empty arrays
            Transformer: [],
            Fuses: [],
            ServicePoint: []
        });

        console.log(arrays);

        if (arrays.Fuses.length === 0) {
            // Do what you need to here.
            console.log("No fuses");
        }
    });
}

var data = [{
        "displayFieldName": "",
        "fieldAliases": {
            "OBJECTID": "OBJECTID"
        },
        "fields": [{
            "name": "OBJECTID",
            "type": "esriFieldTypeOID",
            "alias": "OBJECTID"
        }],
        "features": [{
                "attributes": {
                    "OBJECTID": 649
                }
            },
            {
                "attributes": {
                    "OBJECTID": 665
                }
            },
            {
                "attributes": {
                    "OBJECTID": 762
                }
            }
        ]
    },
    {
        "displayFieldName": "",
        "fieldAliases": {
            "display": "display",
            "OBJECTID": "OBJECTID"
        },
        "geometryType": "esriGeometryPoint",
        "spatialReference": {
            "wkid": 4326,
            "latestWkid": 4326
        },
        "fields": [{
                "name": "display",
                "type": "esriFieldTypeString",
                "alias": "display",
                "length": 50
            },
            {
                "name": "OBJECTID",
                "type": "esriFieldTypeOID",
                "alias": "OBJECTID"
            }
        ],
        "features": [{
                "attributes": {
                    "display": "Transformer",
                    "OBJECTID": 1537
                },
                "geometry": {
                    "x": -88.17602806699995,
                    "y": 41.78431233100008
                }
            },
            {
                "attributes": {
                    "display": "Transformer",
                    "OBJECTID": 1591
                },
                "geometry": {
                    "x": -88.17546081099994,
                    "y": 41.783341919000065
                }
            }
        ]
    },
    {
        "displayFieldName": "",
        "fieldAliases": {
            "display": "display",
            "OBJECTID": "OBJECTID"
        },
        "geometryType": "esriGeometryPoint",
        "spatialReference": {
            "wkid": 4326,
            "latestWkid": 4326
        },
        "fields": [{
                "name": "display",
                "type": "esriFieldTypeString",
                "alias": "display",
                "length": 50
            },
            {
                "name": "OBJECTID",
                "type": "esriFieldTypeOID",
                "alias": "OBJECTID"
            }
        ],
        "features": [{
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13597
                },
                "geometry": {
                    "x": -88.17599727899994,
                    "y": 41.78465526100007
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13598
                },
                "geometry": {
                    "x": -88.17595382899998,
                    "y": 41.78455803400004
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13599
                },
                "geometry": {
                    "x": -88.17582231499995,
                    "y": 41.78435312600004
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13600
                },
                "geometry": {
                    "x": -88.17561004899994,
                    "y": 41.784005335000074
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13601
                },
                "geometry": {
                    "x": -88.17557576699994,
                    "y": 41.78393182000008
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13602
                },
                "geometry": {
                    "x": -88.17535967199996,
                    "y": 41.78352876900004
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13603
                },
                "geometry": {
                    "x": -88.17534426199995,
                    "y": 41.78340020400003
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13649
                },
                "geometry": {
                    "x": -88.17450698899995,
                    "y": 41.78350136200004
                }
            },
            {
                "attributes": {
                    "display": "Service Point",
                    "OBJECTID": 13650
                },
                "geometry": {
                    "x": -88.17435162999999,
                    "y": 41.783597986000075
                }
            }
        ]
    }
];


process();

Upvotes: 1

Related Questions