I have created three functions
, that can make two separate arrays
with the matching keys
and the matching properties
. I would like to use those two arrays
to create a new object
with all of the matching pairs. Below I included the functions
I have so far.
I am a beginner in Javascript
( which is most likely already apparent) so if you can explain each step thoroughly and not get to complex I would greatly appreciate it.
function contains(array, obj_to_check){
for(var i = 0; i < array.length; i ++) {
if (array[i] == obj_to_check) {
return true;
}
}
}
function sharedKeys(obj1, obj2) {
var obj1Keys = Object.keys(obj1);
var obj2Keys = Object.keys(obj2);
var sharedArray = [];
for( var x = 0; x < obj1Keys.length; x++){
if (contains(obj2Keys, obj1Keys[x])){
sharedArray.push(obj1Keys[x]);
}
}
return sharedArray;
}
function sharedProperties(obj1, obj2) {
var obj1Props = [];
var obj2Props = [];
var propertiesArray = [];
for(var i in obj1)
obj1Props.push(obj1[i]);
for(var x in obj2)
obj2Props.push(obj2[x]);
for (var y = 0; y < obj1Props.length; y ++){
if(contains(obj1Props, obj2Props[y])){
propertiesArray.push(obj2Props[y])
}
}
return propertiesArray;
}