0

I have following JSON data,

var configs =   [{
                    "port" : "3003", 
                    "mongoURI" : "mongodb://localhost:27017/serviceRequest", 
                    "MQ" : "RMQ", 
                    "logLevel" : "2", 
                    "version" : "1.1", 
                    "created" : "03-06-2018 03:11:00 PM", 
                    "active" : "N"
                }, 
                {
                    "port" : "3004", 
                    "mongoURI" : "mongodb://localhost:27017/serviceRequest", 
                    "MQ" : "IMQ", 
                    "logLevel" : "1", 
                    "version" : "1.2", 
                    "created" : "07-06-2018 03:11:00 PM", 
                    "active" : "Y"
                }, 
                {
                    "port" : "3003", 
                    "mongoURI" : "mongodb://localhost:27017/serviceRequest", 
                    "MQ" : "Apache Cafka", 
                    "logLevel" : "3", 
                    "version" : "1.3", 
                    "created" : "03-06-2018 03:11:00 PM", 
                    "active" : "Y"
                }, 
                {
                    "port" : "3003", 
                    "mongoURI" : "mongodb://localhost:27017/serviceRequest", 
                    "MQ" : "RMQ", 
                    "logLevel" : "3", 
                    "version" : "1.4", 
                    "created" : "03-06-2018 03:11:00 PM", 
                    "active" : "Y"
                }]

And, I have following dynamic json object.

var cloudParam = {
        "MQ" : "RMQ",
        "logLevel" : "2"
    }

Dynamic means, key-Value pair in cloudParam is not fixed. Rightnow it have MQ & LogLevel, but next time it will be, MQ,loglevel & version - Basically it is not fixed.

So, i want to find json object which contains cloudParam. I tried below code, but i am not getting json obj.

for(i = 0; i < configs.length ; i++){
    var configItem = configs[i];
    for (var item in cloudParam) {        
        if(item in configItem && configItem[item] === cloudParam[item]){
            console.log("Present");
        } else {
            console.log("Not Present");
        }
    }    
}

EDIT:@Aks, thanks for answer, If i want to find index of matched document, how to find it?

1 Answer 1

1

Use filter and every. You can use .find instead of .filter if you only need one match.

var cloudParam = {
  MQ: "RMQ",
  logLevel: "2"
};

console.log(
  configs.filter(config =>
    Object.keys(cloudParam).every(key => config[key] === cloudParam[key])
  )
);
Sign up to request clarification or add additional context in comments.

3 Comments

If i want to find out index of matching object, then how to find it.
if there [ logLevel: "2" ] is present two times in the original array i.e. in config array, it will return 2 objects, I want exact match.
Like I said in the answer: "You can use .find instead of .filter if you only need one match."

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.