Reputation: 319
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?
Upvotes: 0
Views: 1755
Reputation: 169184
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])
)
);
Upvotes: 1