jsPlayer
jsPlayer

Reputation: 1245

JavaScript (Angular) array not returning?

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

Upvotes: 0

Views: 66

Answers (1)

FuriousD
FuriousD

Reputation: 854

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

Upvotes: 2

Related Questions