-1

I am trying to get an atribute from every object in an array. The object is called data and looks like this

[
  { asin: 'B07PC4SF1K' }, { asin: 'B088NFQDSH' },
  { asin: 'B088NKZT13' }, { asin: 'B086H4C8CX' },
  { asin: 'B086QC62JR' }, { asin: 'B088N774B7' },
  { asin: 'B07XDBG6K3' }, { asin: 'B086H3HH5V' },
  { asin: 'B08FYTSXGQ' }, { asin: 'B08BBXK6RQ' },
  { asin: 'B0843HCT63' }, { asin: 'B08FYV84JT' },
  { asin: 'B084D89DBF' }, { asin: 'B086Q3FPN9' },
  { asin: 'B00FRSYS12' }, { asin: 'B07P6Y8L3F' },
  { asin: 'B08BX7LWXS' }, { asin: 'B082XYGR2C' },
  { asin: 'B087LY84RT' }, { asin: 'B0735QBGCL' },
  { asin: 'B088NFQDSH' }, { asin: 'B07GFGJPNQ' }
]

then I am trying to loop through the objects and get value of each asin(for example B08BBXK6RQ)

console.log(data);
for (ASIN in data) {
  console.log(ASIN.asin);
  urls = await requestQueue.addRequest({
    url: `https://www.amazon.com/dp/${ASIN.asin}`,
    userData: {
      label: "ITEM",
    },
  });
  console.log(urls);
}

and in the end I am logging twice the ASIN.asin(console.log(ASIN.asin),console.log(urls)). In both cases I get undefined on the place of ASIN.asin. Any idea guys? Appreciate any advice

Reyno
  • 6,119
  • 18
  • 27
jenlee123
  • 133
  • 1
  • 10
  • 3
    Instead of using `for:in`, try a traditional for-loop e.g. `for (let i = 0; i < data.length; i++)`. I do not think iterators support asynchronous logic. Try using [for await...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) maybe? – Mr. Polywhirl Jan 13 '21 at 13:50
  • 2
    Try a `for of` or normal `for` loop – Reyno Jan 13 '21 at 13:52
  • Replace your for w/ `for (const ASIN in data)` – Andrei Radu Jan 13 '21 at 13:52
  • 1
    _“The object is called data”_ - no, the _array_ is called data. And by going `for-in` over an array, you are actually iterating over its properties, such as `length`. That length is a number, and it does not have a property called `asin`. – CBroe Jan 13 '21 at 13:53
  • Does this answer your question? [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Liam Jan 13 '21 at 13:53
  • Does this answer your question? [What is the difference between ( for... in ) and ( for... of ) statements in JavaScript?](https://stackoverflow.com/questions/29285897/what-is-the-difference-between-for-in-and-for-of-statements-in-jav) – Ivar Jan 13 '21 at 13:56

1 Answers1

0

Try calling asynchronous (async) methods inside of a traditional for-loop.

const data = [
  { asin: 'B07PC4SF1K' }, { asin: 'B088NFQDSH' },
  { asin: 'B088NKZT13' }, { asin: 'B086H4C8CX' },
  { asin: 'B086QC62JR' }, { asin: 'B088N774B7' },
  { asin: 'B07XDBG6K3' }, { asin: 'B086H3HH5V' },
  { asin: 'B08FYTSXGQ' }, { asin: 'B08BBXK6RQ' },
  { asin: 'B0843HCT63' }, { asin: 'B08FYV84JT' },
  { asin: 'B084D89DBF' }, { asin: 'B086Q3FPN9' },
  { asin: 'B00FRSYS12' }, { asin: 'B07P6Y8L3F' },
  { asin: 'B08BX7LWXS' }, { asin: 'B082XYGR2C' },
  { asin: 'B087LY84RT' }, { asin: 'B0735QBGCL' },
  { asin: 'B088NFQDSH' }, { asin: 'B07GFGJPNQ' }
];

class Queue {
  async addRequest({ url, userData={} }) {
    const queryString = Object.entries(userData)
      .map(param => param.join('=')).join('&');
    return `${url}${queryString ? `?${queryString}` : ''}`;
  }
}

const main = async () => {
  const requestQueue = new Queue();

  for (let i = 0; i < data.length; i++) {
    const { asin } = data[i];
    const urls = await requestQueue.addRequest({
      url: `https://www.amazon.com/dp/${asin}`,
      userData: { label: "ITEM" }
    });
    console.log(urls);
  }
}

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132