Javascript:对象数组,异步更改每个对象中的值
所以,我知道以前有人问过这个问题,我也尝试过其他答案,比如Javascript:对象数组,异步更改每个对象中的值,javascript,object,asynchronous,cloudinary,Javascript,Object,Asynchronous,Cloudinary,所以,我知道以前有人问过这个问题,我也尝试过其他答案,比如.map,(函数(post){async})(值),但我仍然被卡住了 因此,我有一个对象数组和一个for循环: var postsData = [{thumbnail: www.website.com/image.jpg}, {thumbnail: www.website.com/image.jpg}, {thumbnail... etc}]; for (let i = 0; i < 3; i++) { let thumbna
.map
,(函数(post){async})(值)
,但我仍然被卡住了
因此,我有一个对象数组和一个for循环:
var postsData = [{thumbnail: www.website.com/image.jpg}, {thumbnail: www.website.com/image.jpg}, {thumbnail... etc}];
for (let i = 0; i < 3; i++) {
let thumbnail = postsData[i].thumbnail;
Cloudinary.uploader.upload(thumbnail, function(result){
// not sure what to do here
// result comes back as an object, and I want to change each thumbnail in
// the postsData array to be result.public_id
}, {transformation:[{}]});
} // go through all the items in the array
// do something with "updated" postsData array
var postsData=[{缩略图:www.website.com/image.jpg},{缩略图:www.website.com/image.jpg},{缩略图…等}];
for(设i=0;i<3;i++){
设thumbnail=postsData[i]。thumbnail;
Cloudinary.uploader.upload(缩略图、函数(结果){
//我不知道该怎么办
//结果作为一个对象返回,我想更改中的每个缩略图
//postsData数组是result.public\u id
},{transformation:[{}]});
}//遍历数组中的所有项
//使用“更新的”postsData数组执行某些操作
很明显,一个例子会很有帮助,更改值涉及一些异步函数。将数组中对象的“缩略图”属性设置为结果。public\u id
。创建一个函数,其中预期参数是postsData
数组中的当前对象,通过将函数引用传递给upload
函数,使用function.prototype.bind()传递数组prop
对象的当前对象,来设置对象的“缩略图”
属性
plnkr据我所知,您正试图在数组中循环,并对其每个元素执行异步函数。我要做的是使用Promise.js(请参阅)。所以代码看起来像这样:
// create an array for the promises
const PromiseArr = [];
postsData.map(d => {
PromiseArr.push(
// do async actions and push them to array
new Promise((resolve, reject) => {
Cloudinary.uploader.upload(thumbnail,(result) => {
// return the result
resolve(result);
});
})
);
})
// do all async actions and store the result in result array
const result = PromiseArr.all();
您的postsData
无效-可能这是一个起点(值应为字符串)尝试了该方法,但不幸的是,当我在for之后调用postsData时,缩略图仍然相同…”尝试了该方法,但不幸的是,当我在for之后调用postsData时,缩略图仍然相同。“您什么时候在控制台上记录了postsData
Cloudinary.uploader.upload
callback函数异步设置result
,是吗<代码>让缩略图=postsData.缩略图
可能在Cloudinary.uploader.upload()之前需要,因为thumbnail
在javascript
中是undefined
atatjavascript
at Answer然后我调用console.log(postsData)代码>在for prop循环之后和之外,您的示例没有使用for(i++)循环,所以我删除了该循环。。。但我最终还是得到了相同的postsData数组。@Daltron您可以定义一个命名函数,使用function.prototype.bind()
传递prop
作为第一个参数,在函数中,设置this[“缩略图”]=result.public_id
;例如var n=0,len=postsData.length;函数handleData(result){this[“缩略图”]=result.public_id;++n;if(n==len)complete(postsData);};Cloudinary.uploader.upload(缩略图,handleData.bind(prop),{transformation:[{}]})
,请参阅。谢谢,这看起来很有希望(双关语),我要去看《火星攻击》,但明天早上我会尝试你的解决方案并发布结果。
// create an array for the promises
const PromiseArr = [];
postsData.map(d => {
PromiseArr.push(
// do async actions and push them to array
new Promise((resolve, reject) => {
Cloudinary.uploader.upload(thumbnail,(result) => {
// return the result
resolve(result);
});
})
);
})
// do all async actions and store the result in result array
const result = PromiseArr.all();