如何动态访问API调用响应的数组索引

时间:2019-07-02 15:12:22

标签: javascript arrays fetch-api

我正在使用Yelp API返回业务列表,响应是一个对象,该对象具有一个称为“ businesses”的键,该键的值是一个由20个项目组成的数组,这些项目是对象,但是我不确定如何在不像array [0]那样对其进行硬编码的情况下访问数组中的每个项目,我想使用一个变量来动态访问数组的索引,这样我就不必输入0,1等,并且我能够要输入获取此数组中每个项目的ID或位置所需的索引,下面是一个示例business[vartoaccesseachindex].id。这样做的原因是因为我正在使用.map()函数返回具有所需值的对象的新数组。

这是响应中的对象

…}
​
businesses: (20) […]
​​
0: Object { id: "uJZgom8HpK74xz8IojpT0A", alias: "pizzeria-beddia-philadelphia", name: "Pizzeria Beddia", … }
​​
1: Object { id: "M_EpyAH1CZZVlhxfYBLOqg", alias: "nomad-pizza-company-philadelphia", name: "Nomad Pizza Company", … }
​​
2: Object { id: "wuH4TPUo8oJo4E59xZKsNg", alias: "and-pizza-philadelphia", name: "&pizza", … }
​​
3: Object { id: "VSDdUwXktnvL_O0meKv7MQ", alias: "alice-pizza-philadelphia", name: "Alice Pizza", … }
​​
4: Object { id: "jyBbsc6LrIwdJYD9jFuiaw", alias: "aldo-and-manny-pizza-and-pasta-philadelphia", name: "Aldo and Manny Pizza and Pasta", … }
​​
5: Object { id: "cFSyJluKa2SHtgMMvlx6SQ", alias: "angelos-pizzeria-philadelphia", name: "Angelo's Pizzeria", … }
​​
6: Object { id: "jNxvJeAUzzpC7zW3UN78HA", alias: "pizza-shackamaxon-philadelphia", name: "Pizza Shackamaxon", … }
​​
7: Object { id: "4PyqPHAiFopyZZlLcLQuVg", alias: "d-oliva-evoo-pizza-and-grill-philadelphia", name: "D'Oliva evoo Pizza & Grill", … }
​​
8: Object { id: "0-JRAY0LpBazDuA9761U5w", alias: "pizzeria-vetri-philadelphia-6", name: "Pizzeria Vetri", … }
​​
9: Object { id: "IzyQVIJG8JAnOiRQPb0-wg", alias: "rione-philadelphia", name: "Rione", … }
​​
10: Object { id: "7pwZZVVlYCxQvVdd8Q03wg", alias: "la-rosa-pizzeria-philadelphia", name: "La Rosa Pizzeria", … }
​​
11: Object { id: "u7_3L1NBWgxhBM_B-cmmnA", alias: "pizzeria-vetri-philadelphia-5", name: "Pizzeria Vetri", … }
​​
12: Object { id: "bmJgvpuf2GBXd62ELK0Q2w", alias: "luigis-pizza-fresca-philadelphia", name: "Luigi's Pizza Fresca", … }
​​
13: Object { id: "5Fm6khB-De-OdqATep9J9Q", alias: "nypd-pizza-philadelphia", name: "NYPD Pizza", … }
​​
14: Object { id: "Mna6U7kUX36l8-dpDt-U-A", alias: "gianfranco-pizza-rustica-philadelphia", name: "Gianfranco Pizza Rustica", … }
​​
15: Object { id: "TKPAyOWcexkpVHPCdYTNmQ", alias: "spuntino-wood-fired-pizza-philadelphia", name: "Spuntino Wood Fired Pizza", … }
​​
16: Object { id: "sL6fC0P4C-gyL4E5gacUeQ", alias: "lazaros-pizzeria-and-grill-philadelphia-2", name: "Lazaro's Pizzeria & Grill", … }
​​
17: Object { id: "I1H5s9cVCGh1nH-4bhsSDA", alias: "santuccis-original-square-pizza-philadelphia-2", name: "Santucci's Original Square Pizza", … }
​​
18: Object { id: "6ajnOk0GcY9xbb5Ocaw8Gw", alias: "barbuzzo-philadelphia", name: "Barbuzzo", … }
​​
19: Object { id: "vC98a0bIcSDVfX3dOuuAkQ", alias: "yummy-pizza-philadelphia", name: "Yummy Pizza", … }

这是我的JS代码,这是.map()函数,用于返回包含提取信息的新对象。提取的信息将存储在一个键/值对中,因此id必须访问business [vartoaccesseachindex] id,imageSrc必须访问business [vartoaccesseachindex] .imageSrc。

jsonResponse.businesses.map( business => {
            return {
                id: business[vartoaccesseachindex],
                imageSrc: business,
                name: business,
                address:  business,
                city: business,
                state:  business,
                zipCode:  business,
                category: business,
                rating: business,
                reviewCount:  business,
            }
          });

任何答案都是很合理的。

1 个答案:

答案 0 :(得分:0)

map回调已经提供了您需要的对象。如果确实需要索引,则将它作为第二个参数提供。

const jsonResponse = {
  more: {stuff: 'here'},
  businesses: [
    { id: "uJZgom8HpK74xz8IojpT0A", alias: "pizzeria-beddia-philadelphia", name: "Pizzeria Beddia"},
    { id: "M_EpyAH1CZZVlhxfYBLOqg", alias: "nomad-pizza-company-philadelphia", name: "Nomad Pizza Company"},
    { id: "wuH4TPUo8oJo4E59xZKsNg", alias: "and-pizza-philadelphia", name: "&pizza"},
    // ... 
  ]
}

const myData = jsonResponse .businesses .map ( (business, index) => ({
  id: business.id,
  index: index,
  name: business.name,
  // ... 
}))

console .log (
  myData
)

不过,我的问题是,是否要使用属性列表复制整个对象。如果是这样,何必呢?为什么不只使用数组中可用的business对象呢?