user2405589
user2405589

Reputation: 968

Javascript array push

I am trying to iterate through an array and push the contents from the array to another array if they satisfy some basic condition. Really simple stuff. PLease help me.

Getting list of DBs in MongoDB

>>var dbs = db.getMongo().getDBNames();

>>dbs
[ "admin", "newdb", "newdb2", "local", "test" ]

Get array length:

var arrayLength = dbs.length;

Creating an empty array

var databases = []

Pushing to the databases array content from dbs array if content is not equal to 'admin','local' or 'test'

for (var i = 0 ; i < arrayLength; i++) { 
  if (!(dbs[i] == "admin") || !(dbs[i] == "local") || !(dbs[i] == "test")) {
    databases.push(dbs[i]);
  }
}

Expecting only "newdb" and "newdb2" to be in the databases array. But everything is being pushed

>databases
[ "admin", "newdb", "newdb2", "local", "test" ]

What's going on here? Only newdb and newdb2 needs to be in the "databases" array.

Upvotes: 1

Views: 141

Answers (4)

Josh
Josh

Reputation: 18690

I did not see it mentioned, but I suggest looking into Array.prototype.filter. This creates the second array for you, and also manages the iteration over the values of the first array. I would also avoid a big set of conditions, and just rely on Array.prototype.includes to do the work for you.

var dbs = ["admin", "newdb", "newdb2", "local", "test"];
var unwantedValues = ["admin", "local", "test"];
var databases = dbs.filter(function(value) {
  return !unwantedValues.includes(value);
});

Edit, you could also think of this as creating a complement, like in math.

// Create this as a utility function in your code
// Return a new array object that contains all values from arrayA 
// that are not in arrayB 
function complement(arrayA, arrayB) {
  return arrayA.filter(function(value) {
    return !arrayB.includes(value);
  });
}

// Then call it
var databases = complement(dbs, unwantedValuesArray);

Upvotes: 0

user6576367
user6576367

Reputation:

Easy stuff. :-)

var dbs = [ "admin", "newdb", "newdb2", "local", "test" ];
var filterShema = new RegExp(/admin|local|test/);
var databases = dbs.filter(function(db) { return !filterShema.test(db); });

> databases
["newdb", "newdb2"]

Upvotes: 2

Lincoln Bergeson
Lincoln Bergeson

Reputation: 3451

Use this instead:

var dbs = [ "admin", "newdb", "newdb2", "local", "test" ];
var arrayLength = dbs.length;
var databases = [];
for (var i = 0 ; i < arrayLength; i++){ 
    if ((dbs[i] != "admin") && (dbs[i] != "local") && (dbs[i] != "test")) 
        {
         databases.push(dbs[i])
        }
}
console.log(databases);

Upvotes: 1

Trott
Trott

Reputation: 70075

You should use && rather than || in this condition:

(!(dbs[i] == "admin") || !(dbs[i] == "local") || !(dbs[i] == "test")) 

The way it is written above, it is "value is not A OR not B OR not C" which will be true for everything. (A is not B, for example.)

You mean it to say "value is not A AND not B AND not C".

(!(dbs[i] == "admin") && !(dbs[i] == "local") && !(dbs[i] == "test")) 

Upvotes: 4

Related Questions