0

so i am working on emptying a property (polygon) from an array of objects enter image description here

 deleteAllZones = () => { let assetVal = this.asset.$$state.value let mapId = this.siMapUtils.mapId; console.log('load',assetVal) var polygons = assetVal.forEach(function (a) { console.log('a.poly',a.polygon) return a.polygon }) console.log('test',polygons); let message = this.$filter('translate')('SI-MESSAGES.DELZONE-MSG'); let title = this.$filter('translate')('SUBHEADER.DELZONE-TITLE'); let button = this.$filter('translate')('BUTTON.DELETE'); this.siAlertDialog.confirm(message, title, button) .then(()=>{ this.siGeography.removePolygonFromMap('showAllAreas', mapId).then(()=>{ this.toastIt.simple(this.$filter('translate')('SI-MESSAGES.ZONE-DELETED')) }) }) }

in var polygons i was able to iterate and console just the polygon property of all the object in the array (a.polygon) . But its returning undefined (return a.polygon) when i console 'polygons', but console.log('a.poly',a.polygon) is showing the array in the consoleenter image description here

 var polygons = assetVal.forEach(function (a) { console.log('a.poly',a.polygon) return a.polygon }) 

eventually i want to say polygons = null when this function execute

    1 Answer 1

    2

    The function you're looking for is Array#Map. This will construct an array with each returned value from the callback function.

    Modify this block of code to read:

    var polygons = assetVal.map(function (a) { console.log('a.poly',a.polygon) return a.polygon }) 

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map?v=control

    2
    • i see that works, so as i described above i am creating this is function to delete the polygons. if i have a service like this 'this.siAsset.updateComponent( _polygons)' to delete the polygons, would i be able to say let _polygons = assetVal.map(function (a) { return a.polygon = [] //empty array }) and pass _polygons as a parameter in that service?
      – jsPlayer
      CommentedJun 20, 2017 at 1:15
    • @AnuRajan that will create an array of empty arrays. If you'd like to maintain the structure of each a object, but empty out a.polygon, then you should do: a.polygon = []; return a; inside the map function. First we're modifying the polygon property of a, then we're returning that modified a
      – FuriousD
      CommentedJun 21, 2017 at 0:11

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.