Node.js中的jQuery.each()?

我必须在JSON数组中循环来获取node一些信息,但我只知道如何在jQuery中使用$.each() 。 所以我想知道node.js中的$.each jQuery函数是否有其他select?

你可以使用这个

 for (var name in myobject) { console.log(name + ": " + myobject[name]); } 

myobject可能是你的JSON数据

看看这里的答案: 使用node.js循环浏览JSON

你应该使用native for ( key in obj )迭代方法:

 for ( var key in yourJSONObject ) { if ( Object.prototype.hasOwnProperty.call(yourJSONObject, key) ) { // do something // `key` is obviously the key // `yourJSONObject[key]` will give you the value } } 

如果你正在处理数组,只需使用常规的for循环:

 for ( var i = 0, l = yourArray.length; i < l; i++ ) { // do something // `i` will contain the index // `yourArray[i]` will have the value } 

或者,您可以使用数组的本地forEach方法, 这是稍微慢一些 ,但更简洁:

 yourArray.forEach(function (value, index) { // Do something // Use the arguments supplied. I don't think they need any explanation... }); 

在nodejs中,我find了Array.forEach(callback)以最好地满足我的需求。 它像jQuery一样工作:

 myItems.forEach(function(item) { console.log(item.id); }); 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

jQuery只是JavaScript,你可以自己做循环。

我不知道你正在循环的JSON数组的结构,但你可以使用for..in方法来获取对象的每个属性。

所以你会做这样的事情:

  for( var i = 0; len = jsonArray.length; i < len; i++) { for(var prop in jsonArray[i]) { //do something with jsonArray[i][prop], you can filter the prototype properties with hasOwnProperty } } 

你也可以使用Array提供的forEach方法,其工作方式与jQuerys .each()

祝你好运!