1

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.

1
  • Also you can just use if(dbs[i] != "admin" && dbs[i] != "local" && dbs[i] != "test"). Commented Jun 6, 2017 at 23:07

4 Answers 4

4

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")) 
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the delayed reply! But yes, you were right!
2

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"]

4 Comments

Regex is not easy if you're new to programming, and neither is filter.
^ I would disagree about filter. The word is idiomatically what you are doing, and it's very commonly used. Also that particular regex is pretty clear.
A correct answer doesn't care how new OP is to programming if it's a good answer. We can't be expected to dilute knowledge, only try and teach where possible
@Damon +1 for ^ :-D
1

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);

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.