Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何遍历javascript对象?_Javascript_Api - Fatal编程技术网

如何遍历javascript对象?

如何遍历javascript对象?,javascript,api,Javascript,Api,我正在进行服务器端API调用。用户可以输入成分并进行搜索。提交数据时,我在终端上看到以下API调用: “” 我希望它看起来像这样: “” 这是我的密码: router.get("/whatCanIMake", function(request, response){ var inputIngredients = request.query; var ingredientString = ""; console.log(inputIngredients); for

我正在进行服务器端API调用。用户可以输入成分并进行搜索。提交数据时,我在终端上看到以下API调用:

“”

我希望它看起来像这样:

“”

这是我的密码:

router.get("/whatCanIMake", function(request, response){

    var inputIngredients = request.query;
    var ingredientString = "";
    console.log(inputIngredients);
    for (var i = 0; i<inputIngredients.length; i++){
        ingredientString += inputIngredients[i] + ",";
    }

    var api = "http://www.recipepuppy.com/api/?i=" + inputIngredients + "";
    console.log(api);
    console.log("inputingredients", inputIngredients);

    request.get(api, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
    });
});
router.get(“/whatCanIMake”,函数(请求、响应){
var inputIngredients=request.query;
var ingredientString=“”;
控制台日志(输入文件);
对于(var i=0;i
router.get(“/whatCanIMake”),函数(请求、响应){
var inputIngredients=request.query;
var ingredientString=“”;
控制台日志(输入文件);

//对于(var i=0;i我将使用Lodash 0.forEach方法:


for(对象中的var属性){if(object.hasOwnProperty(property)){//do stuff}
InputgDients的内容是什么?它只是一个可能成分的字符串数组吗?还是一个对象数组?它看起来像对象。如果不知道这些对象的结构,那么就不可能说出要为您的查询提取哪个属性。因此,我们需要查看
InputgDients的结构de>。这是我的终端的console.log用于输入的变量{0':'milk','1':'sugar','2':'water','3':'mour'}。您正在看到
[object]
因为您正在追加
inputingredinents
,这是一个数组。您想追加
ingredintstring
。我在终端中得到了这个作为返回:Object]inputingredinents{'0':'egg','1':'milk','2':'frule','3':'water'}为什么会这样?生成后您甚至没有使用
ingredientString
。我在提交后注意到同样的情况,并将其更改为:var api=“+ingredientString+”;我将其作为控制台获得。log:console.log()console.log(输入变量{0':'egg',1':'butter',2':'milk})@Mike C是的,对不起,我只是纠正了他的迭代代码,没有检查整个代码。我认为他在获取代码方面遇到了问题。
router.get("/whatCanIMake", function(request, response){

var inputIngredients = request.query;
var ingredientString = "";
console.log(inputIngredients);
//for (var i = 0; i<inputIngredients.length; i++){
//   ingredientString += inputIngredients[i] + ",";
//}

for (var property in inputIngredients)
{
 if (inputIngredients.hasOwnProperty(property)) {
 ingredientString += property + ",";
 }
}
var ingredientString = "";

_.forEach(inputIngredients, function(value,key){

  if(ingredientString.length != ""){
    ingredientString += "," + value;
  }
  else {
    ingredientString = value;
  }

});