Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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
Arrays NodeJS-从数组中删除特定项_Arrays_Node.js - Fatal编程技术网

Arrays NodeJS-从数组中删除特定项

Arrays NodeJS-从数组中删除特定项,arrays,node.js,Arrays,Node.js,我正在从mongodb获取所有集合的列表,以数组的形式 mongoose.connection.db.listCollections().toArray(function (err, names) { if (err) { console.log(err); } console.log(names); 输出: [ { name: 'system.indexes' }, { name: 'books' }, ... 我想从数组中删除system.index

我正在从mongodb获取所有集合的列表,以数组的形式

mongoose.connection.db.listCollections().toArray(function (err, names) {
   if (err) {
      console.log(err);
   }
   console.log(names);
输出:

[ { name: 'system.indexes' },
  { name: 'books' },
  ...
我想从数组中删除
system.index
。我尝试了一些功能,如:

  • 拼接
  • 当班
  • 不带功能的下划线的

老实说,我甚至不知道它们是否用于此目的。

要删除name等于
system的对象,请按以下步骤执行操作:

mongoose.connection.db.listCollections().toArray(function (err, names) {
   if (err) {
      console.log(err);
   }else{
     var i;

     for(i=names.length - 1; i >= 0; i-=1){
        if(names[i].name !== 'system.indexes'){
            names.splice(i,1);
        }
     }

     // names now contain all items without the system.indexes
   }

要删除整个对象{name:'system.indexes'}还是仅删除name的属性?@Bernhard删除整个对象。因此,输出结果将是
[{name:'books'},…]
好的,它确实可以工作。但我很困惑。我们真的必须这样做吗?我的意思是使用循环并重新创建数组有点过时。@xperator在纯javascript中你是对的,最好向后迭代。更新了我的答案