1

This question has been asked so many times but I can't make what I want so I ask for your help.

I have 2 arrays checkMyDataSources and lesInfosMachines.

I need to run through checkMyDataSources to check if there is no occurrence of any items in lesInfosMachines.
The content of checkMyDataSources can be something like ["datasource_A","datasource_B","datasource_D","datasource_C"] and the name is linked with the name of each item in lesInfosMachines who contains thing like ["A","B","C","D"].

The problem is that I'm not able to run through all the checkMyDataSources, I mean when the cell A and Amachine are different it call createDataSourcedespite Amachine is maybe in the cell D.

var lesInfosMachines = InfosMachines.find({});
    if(checkMyDataSources.length < 1){
      console.log("there is not datasource, we will create them all");
      callInitDS();
    }else{
      console.log("there is datasource, we will check them");
      lesInfosMachines.forEach(Meteor.bindEnvironment(function(machineInfo) {
        console.log("test machine " + machineInfo.nameMachine)
        for (var i = 0; i < checkMyDataSources.length; i++) {
          console.log("test on " + checkMyDataSources[i].name.split("_")[1]);

          if(checkMyDataSources[i].name.split("_")[1] === machineInfo.nameMachine){
            console.log("Datasource:  " + machineInfo.nameMachine + " already exist." );
          }else{
            if(machineInfo.ipAddr != null){
              console.log("going to create " + machineInfo.nameMachine);
              createDataSource(machineInfo.nameMachine, machineInfo.ipAddr);
            }else{
              console.log("going to create " + machineInfo.nameMachine + 
                          " with a fake @ip because it was null 
                            ONLY FOR TESTING WE NEED TO REMOVE THIS"
                         );
              createDataSource(machineInfo.nameMachine, "myFakeIP");
            }
          };
        }
      }));
      console.log("test finished")
    }

I hope my question is understandable and thank you for the help

[EDIT] that's my output : enter image description here

[EDIT2] to simplify I want to test aMachine on A,B,C,D of checkMyDataSources and if there isn't aMachine in one of those cells (but at the end) then call createDataSource()

3
  • 1
    How about the use of if {<String>.indexOf(<String2>) > -1} ? Commented Feb 13, 2017 at 13:23
  • @reporter what is it going to do ? Commented Feb 13, 2017 at 13:24
  • 1
    I believe it would be better if you write clearly what your inputs are and what you expect your outputs to be. Commented Feb 13, 2017 at 13:30

4 Answers 4

2

have you heard about lodash ?

const _ = require('lodash');

let checkMyDataSources = ["datasource_A","datasource_B","datasource_D","datasource_C"];
let lesInfosMachines = ["A","B","C"];
_.difference(checkMyDataSources, _.map(lesInfosMachines, (elt) => 'datasource_' + elt));

>[ 'datasource_D' ]
Sign up to request clarification or add additional context in comments.

2 Comments

No I have never heard something about that but it looks very interesting I'll take a look about it, I'm sure it could be helpful for some of my use case
it's not useful, it's essential :)
1
const datasources = ["datasource_A", "datasource_B", "datasource_D", "datasource_C"];
const lesInfosMachines = ["A", "D", "C"];
const prefixLength = "datasource_".length
  • If you want to get datasourceswhich are not on lesInfosMachines:

    datasources.filter((d) => lesInfosMachines.every((l) => l !== d.slice(prefixLength)))
    ["datasource_B"]
    
  • If you want to get datasourceswhich are on lesInfosMachines:

    datasources.filter((d) => lesInfosMachines.some((l) => l === d.slice(prefixLength)))
    ["datasource_A", "datasource_D", "datasource_C"]
    
  • If you want it to return trueif there are some of the datasourcesin lesInfosMachines, and false otherwise:

    let otherLesInfosMachines = ["X", "Y", "Z"]
    
    datasources.some((d) => otherLesInfosMachines.some((l) => l === d.slice(prefixLength)))
    false
    
    datasources.some((d) => lesInfosMachines.some((l) => l === d.slice(prefixLength)))
    true
    

By combining functions such as filter, every, and some, you can implement many algorithms very idiomatically, without having rely on difficult to understand, and even semantically meaningless, forloops and indexes.

1 Comment

Woaw it's very nice ! I'll try to use this method for other use case
1

Instead of connecting the two loops, leading to multiple comparsions of the same element, you could use Array.prototype.includes()

This would make your code approximately look like this:

var lesInfosMachines = InfosMachines.find({});
if (checkMyDataSources.length < 1) {
    console.log("there is not datasource, we will create them all");
    callInitDS();
} else {
    console.log("there is datasource, we will check them");
    lesInfosMachines.forEach(Meteor.bindEnvironment(function(machineInfo) {
        console.log("test machine " + machineInfo.nameMachine);
        if (checkMyDataSources.includes("datasource_" + machineInfo.nameMachine) {
                console.log("Datasource:  " + machineInfo.nameMachine + " already exist.");
            } else {
                if (machineInfo.ipAddr != null) {
                    console.log("going to create " + machineInfo.nameMachine);
                    createDataSource(machineInfo.nameMachine, machineInfo.ipAddr);
                } else {
                    console.log("going to create " + machineInfo.nameMachine + " with a fake @ip because it was null ONLY FOR TESTING WE NEED TO REMOVE THIS");
                    createDataSource(machineInfo.nameMachine, "myFakeIP");
                }
            };
        }
    }));
    console.log("test finished")
}

Otherwise you would have to reorganize your loops as in Jeromes answer.

1 Comment

I'm Jerome ^^, I'll try to implement your solution it sounds better than mine
0

I have found a solution by using a boolean like this:

var hasTestedAll = false;
var lesInfosMachines = InfosMachines.find({});
    if(checkMyDataSources.length < 1){
      console.log("there is not datasource, we will create them all");
      callInitDS();
    }else{
      console.log("there is datasource, we will check them");
      lesInfosMachines.forEach(Meteor.bindEnvironment(function(machineInfo) {
        console.log("test machine " + machineInfo.nameMachine)
        for (var i = 0; i < checkMyDataSources.length; i++) {
          console.log("test on " + checkMyDataSources[i].name.split("_")[1]);

              if(checkMyDataSources[i].name.split("_")[1] === machineInfo.nameMachine){
                if(i == checkMyDataSources.length-1){
                  hasTestedAll = true;
                }
                console.log("Datasource:  " + machineInfo.nameMachine + " already exist." );
              }else if(hasTestedAll){
                if(machineInfo.ipAddr != null){
                  console.log("going to create " + machineInfo.nameMachine);
                  createDataSource(machineInfo.nameMachine, machineInfo.ipAddr);
                }else{
                  console.log("going to create " + machineInfo.nameMachine + " with a fake @ip because it was null ONLY FOR TESTING WE NEED TO REMOVE THIS");
                  createDataSource(machineInfo.nameMachine, "myFakeIP");
                }
              };
        }
      }));
      console.log("test finished")
    }

(sorry for the CodeSnipet that doesn't work but I wasn't able to paste the code with a correct alignment)

So it test if I'm in the last cell of checkMyDataSources and if it's the case I allow to call the create method

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.