简体   繁体   English

使代码一次取一个数字并嵌入其中

[英]Make the code take one number at a time and embed in it

I'm trying to use an API in my code that downloads media from Instagram posts.我正在尝试在我的代码中使用 API 从 Instagram 帖子下载媒体。

The response passed by the API is as follows: API传过来的响应如下:

 { post1: { url: 'download url', type: 'media type' }, post2: { url: 'download url', type: 'media type' } }

post1, post2, and so on depending on the number of posts on that link (up to 10 posts). post1、post2 等,具体取决于该链接上的帖子数(最多 10 个帖子)。

i would like it to query and get the url of each of them.我希望它查询并获取每个人的 url。

 var resultsMedia = await apis.insta_post(link)

I tried我试过了

 resultsMedia.post[i].url

(i refers to a for function I tried, for var i = 1; ...) and I was not successful. (我指的是 for function 我试过了,for var i = 1; ...)但我没有成功。

I would like to know how I could make the resultsMidia.post take one number at a time example: resultsMedia.post1.url, resultsMedia.post2.url, resultsMedia.post3.url, and so on.我想知道如何使 resultsMidia.post 一次取一个数字示例:resultsMedia.post1.url、resultsMedia.post2.url、resultsMedia.post3.url 等。

(resultsMedia refers to the API call, post refers to the api response, and url is a parameter also present in the API response) (resultsMedia 指 API 调用,post 指 api 响应,url 是 API 响应中也存在的参数)

You can try this:你可以试试这个:

const postCount=Object.keys(resultsMedia).length;
for (let i = 1;i<=postCount;i++){
   console.log(resultsMedia[`post${i}`])
}

You can iterate using Object.entries() :您可以使用Object.entries()进行迭代:

 const response = { post1: { url: 'download url 1', type: 'media type' }, post2: { url: 'download url 2', type: 'media type' } } Object.entries(response).forEach(([post, data]) => { console.log('---------------') console.log('Post:', post) console.log('Post number:', +post.replace('post', '')) console.log('Data:', data) console.log('Data url:', data.url) console.log('---------------') })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM