0

enter image description here

above i have the picture of my arrays, below code i have mapped 2 sets of array .Purpose of this function is to make '_polygons' empty array. i have a rest service which is 'this.siAsset.updateComponent(_id, _polygons)' that is designed to update each asset. only thing is, i cant seem to pass an array of data to the update component service i have, its designed to take one id and one object as a parameter (if there is a way around , please provide) as you can see by the picture, both _id and _polygons have arrays of id's an arrays of objects. Question is, how can i loop and match to call the rest to go through each id and object instead of calling by writing the code 9 times like this.siAsset.updateComponent(_id[0], _polygons[0]) ...this.siAsset.updateComponent(_id[9], _polygons[9])

  deleteAllZones = () => {
        let assetVal = this.asset.$$state.value
        console.log('tenant', assetVal)
        let _polygons = assetVal.map(function (a) {
             a.polygon = []
             return a
        })
        console.log('a',_polygons)
        let _id = assetVal.map(function (a) {
            let id = a.id
            return id
        })
        console.log('id',_id)
        
        

        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.siAsset.updateComponent(_id, _polygons).then(() => {
                    this.toastIt.simple(this.$filter('translate')('SI-MESSAGES.ZONE-DELETED'))

                }, (err) => {
                    this.siAlertDialog.error();
                }
                )
            })
    }

2

1 Answer 1

1

Your code snippet doesn't work. I've made dummy entries. This should give you an idea. Basically, you need to loop over one array and use the index to find corresponding items in the second

// Mocking data here
let assetVal = [{
    id: 1
  },
  {
    id: 2
  },
  {
    id: 3
  }
]
let _polygons = assetVal.map(function(a) {
  a.polygon = []
  return a
})
//console.log('a', _polygons)
let _id = assetVal.map(function(a) {
  let id = a.id
  return id
})
//console.log('id', _id)
// Mocking done

let updateComponent = (id, polygon) => {
  return new Promise((resolve, reject) => {
    resolve({
      id,
      polygon
    })
  })
}

_id.forEach((id, i) => {
  updateComponent(id, _polygons[i]).then(result => {
    console.log(result)
  })
})

Also, by looking at your code, it doesn't look like your updateComponent method needs id to be passed as a parameter as it is already present in the polygon as well. Maybe you want to refactor that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.