gpt4 book ai didi

javascript - 将新元素附加到数组中的对象

转载 作者:行者123 更新时间:2023-12-03 04:47:55 25 4
gpt4 key购买 nike

我已经对这个循环感到困惑了几天,但无法完全满足我的需要。

我循环搜索结果数组 (mdb_results),并从每个对象中提取 .name 用作 _search 术语Google CSE 图像搜索。

Th CSE 返回另一个数组 (cse),我想将其附加到从中提取 _search 术语的对象 (mdb_results[i]).

router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
db.collection('mycatalog')
.find({$text: {$search:"FOO" }})
.toArray(function(err, mdb_results){
for (var i=0; i<mdb_results.length; i++){
var _search = mdb_results[i].name ;

ImageSearch(_search, function(cse){

// How to add cse.img to mdb_results[i].images ??
// mdb_results[i].images = cse;
// gives undefined

},0,2);
};
res.send(mdb_results);
});
});
});

我的初始 mdb_results 看起来像这样。

[{"name":"FOO"},{"name":"FOOOO"}]

我正在努力实现这样的目标,

[{"name":"FOO", 
"images":[{"img1":"http://thumbnail1"},{"img2":"http://thumbnail2"}]
},
{"name":"FOOOO",
"images":[{"img1":"http://thumbnaila"},{"img2":"http://thumbnailb"}]
}]

谁能告诉我如何实现这一目标?

谢谢

最佳答案

问题是您期望异步操作同步工作:

router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
db.collection('mycatalog')
.find({$text: {$search:"FOO" }})
.toArray(function(err, mdb_results){
for (var i=0; i<mdb_results.length; i++){
var _search = mdb_results[i].name ;
// This search is asynchronous, it won't have returned by the time
// you return the result below.
ImageSearch(_search, function(cse){

// How to add cse.img to mdb_results[i].images ??
// mdb_results[i].images = cse;
// gives undefined

},0,2);
};
// At this point, ImageSearch has been called, but has not returned results.
res.send(mdb_results);
});
});
});

您将需要使用 Promise 或 async图书馆。

这是一个例子:

router.get('/json', function(req, res, next) {
var ImageSearch = require('node-google-image-search');
MongoClient.connect(url, function(err,db){
db.collection('mycatalog')
.find({$text: {$search:"FOO" }})
.toArray(function(err, mdb_results){
// async.forEach will iterate through an array and perform an asynchronous action.
// It waits for you to call callback() to indicate that you are done
// rather than waiting for it to execute synchronously.
async.forEach(mdb_results, function (result, callback) {
ImageSearch(result.name, function(cse){
// This code doesn't get executed until the network call returns results.
result.images = cse;
// This indicate that you are done with this iteration.
return callback();
},0,2);
},
// This gets call once callback() is called for each element in the array,
// so this only gets fired after ImageSearch returns you results.
function (err) {
res.send(mdb_results);
});
});
});

关于javascript - 将新元素附加到数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42791852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com