1
    this.work[0].minValue = await this.getData().then(
      value => value[0].set.min.X
    )
    this.work[1].minValue = await this.getData().then(
      value => value[0].set.min.Y
    )
    this.work[2].minValue = await this.getData().then(
      value => value[0].set.min.Z
    )
    this.work[3].minValue = await this.getData().then(
      value => value[0].set.min.A
    )
    this.work[4].minValue = await this.getData().then(
      value => value[0].set.min.B
    )
    this.work[5].minValue = await this.getData().then(
      value => value[0].set.min.C
    )

I am trying to write this 6 lines of code inside a loop, which i am not able to do it. Could someone help me out.

var arr = ['X', 'Y', 'Z', 'A', 'B', 'C']
    var i = 0
    var that = this
    this.work.forEach(item => {
      item.minValue = that.getData().then(value => value[0].set.min.that.arr[i])
      i++
    })

i know this is wrong with min.that.arr[i].Please dont say its reason why it not working, Please help me out with the solution.

solomyhansolo
  • 107
  • 2
  • 9

4 Answers4

2

value[0].set.min[arr[i]]

what ashkan suggested fixed my issue.

solomyhansolo
  • 107
  • 2
  • 9
  • If so, Ashkan's comment should be an answer, and this answer should be a comment. I still think you call `getData` too many times uselessly. Some problems need a bit more reflexion than applying the first working suggestion, and careless coders can put themselves in bad situations when it starts to bug.. – Kaddath Jan 23 '20 at 08:50
1

The action should be in the then:

var arr = ['X', 'Y', 'Z', 'A', 'B', 'C']
var i = 0
var that = this
this.work.forEach(item => {
  that.getData().then(value => item.minValue = value[0].set.min.that.arr[i++])
})
Ziv Ben-Or
  • 1,149
  • 6
  • 15
1

If I understand correctly you only need one call to getData outside of the loop, no need to do it at each loop turn. Also, you don't need i as a var, as it can be provided as an argument in the forEach callback function:

var arr = ['X', 'Y', 'Z', 'A', 'B', 'C']
var that = this
await that.getData().then(
    value => {
        that.work.forEach((item, i) => {
            item.minValue = value[0].set.min[arr[i]]
        })
    }
)
Kaddath
  • 5,933
  • 1
  • 9
  • 23
1

The reason this doesn't work is because the then is asynchronous. At the point the callback is made i will have the maximum value for all callbacks (meaning 5 in the provided example).

Instead use the index provided by the forEach loop (second argument provided to the callback function).

const arr = ['X', 'Y', 'Z', 'A', 'B', 'C'];

this.work.forEach((item, index) => {
  const chr = arr[index];
  this.getData().then(value => {
    item.minValue = value[0].set.min[chr];
  });
});

// or using an async arrow function
this.work.forEach(async (item, index) => {
  const chr = arr[index];
  const value = await this.getData();
  item.minValue = value[0].set.min[chr];
});

Note that it's probably not a good idea to make the same asynchronous call each iteration and you most likely want to move the data request before the loop. I'll assume the wrapping function is already using the async keyword, since you also use await this.getData() in your example.

const arr = ['X', 'Y', 'Z', 'A', 'B', 'C'];
const value = await this.getData();

this.work.forEach((item, index) => {
  const chr = arr[index];
  item.minValue = value[0].set.min[chr];
});

You can find more about your i issue here.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52