Reputation: 484
I have 2 JS Objects:
The first one:
One = {
"Sunday": {
1:false,
2:false,
3:true,
4:false, .... until 24. Each item could be either true or false.
},
"Monday": {1:true, 2:false, ....},
//And so on, 7 days of the week
}
The second is just like the first one only its values (true/false) are different.
I want to run a function that can tell me, I don't care how, which items have true
in both Objects. For example, if One.Sunday[2] = true
and Two.Sunday[2] = true
, I want to know that.
I hope I was clear enough.
If possible it would be nice if that same function could accept 3 or more objects also and return the mutual items of all objects.
Thanks, Reuven
Upvotes: 0
Views: 230
Reputation: 10627
Try:
function mutual(){
var a = arguments, o = {};
for(var i in a){
var ai = a[i];
out: for(var n in ai){
o[n] = {}
for(var q in ai[n]){
if(!ai[n][q]){
if(o[n][q]){
delete o[n][q];
}
break out;
}
o[n][q] = true;
}
}
}
for(var i in o){
var c = 0;
for(var n in o[i]){
c++;
}
if(c === 0){
delete o[i];
}
}
return o;
}
// assumes One, Two, Three, Four, Five are Objects
// use firebug for this console.log() or create loops
console.log(mutual(One, Two, Three, Four, Five));
Upvotes: 0
Reputation: 2671
var sundayOne = {1:true,2:false,3:false,4:true};
var mondayOne = {1:true,2:true,3:false,4:false};
var tuesdayOne = {1:false,2:false,3:true,4:true};
var one = {"sunday":sundayOne,"monday":mondayOne,"tuesday":tuesdayOne}; // two objects having
var sundayTwo = {1:true,2:true,3:true,4:true};
var mondayTwo = {1:false,2:false,3:false,4:false};
var tuesdayTwo = {1:true,2:true,3:true,4:true};
var two = {"sunday":sundayTwo,"monday":mondayTwo,"tuesday":tuesdayTwo};
var matchedArr = []
for(each in one) {
var obj = {} //create new object
obj[each] = {} //add new object to store matched each - Sunday, Monday
for(subEach in one[each]) {
//value is false so we have to check with undefined
if(two[each][subEach] != undefined) {
if(one[each][subEach] == two[each][subEach]) {
console.log('Match found at ', each, subEach, one[each][subEach])
obj[each][subEach] = one[each][subEach]
}
}
}
matchedArr.push(obj)
}
console.log(matchedArr)
//output
[Object, Object, Object]
0: Object
sunday: Object
1: true
4: true
1: Object
monday: Object
3: false
4: false
2: Object
tuesday: Object
3: true
4: true
length: 3
Upvotes: 1
Reputation: 25882
I got something working here for array of objects it will return a array of mutual.
Two Objects
var sunday = {1:true,2:false,3:false,4:true};
var monday = {1:true,2:true,3:false,4:false};
var tuesday = {1:false,2:false,3:true,4:true};
var one = {"sunday":sunday,"monday":monday,"tuesday":tuesday}; // two objects having
var two = {"sunday":sunday,"monday":monday,"tuesday":tuesday};
Filtering Mutual :-
function getMutuals (objects) { //get an array of objects
var mutuals = [];
objects.forEach(function(obj,index){
mutuals[index] = {};
for(var key in obj){
mutuals[index][key] = []; //it will create key like sunday,monday in each obj
var day = obj[key]; // get that key
for(var i=1;i<25;i++){
if(day[i]) //if day[i] is present
mutuals[index][key].push(i); //push that i into array of day
}
}
});
return mutuals;
}
you can call this function with arry of objects like following
var mutuals = getMutuals([one,two]);
console.log(mutuals)
Upvotes: 0