Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var target=[{"key1": "value1", "key2": "value22","obj":{"key3": "value3"}},{"key4": "value4"}]
- var refer=[{"key1": "value1"}, {"key2": "value2"},{"key5": "value5"}]
- var referMap=new Map();
- var ansMap=new Map();
- //building refer map
- for(ind in refer){
- for(key in refer[ind]){
- referMap[key]=refer[ind][key];
- }
- }
- function iteratable(tgtObj){
- if(typeof tgtObj ==='array'||typeof tgtObj ==='object')
- {
- return true
- }
- else{
- return false;
- }
- }
- function find(tgtObj,tgtKey){
- if(iteratable(tgtObj)){
- for(key in tgtObj){
- if(iteratable(tgtObj[key])){
- let ansFound=find(tgtObj[key],tgtKey);
- if(ansFound){
- return ansFound
- }//otherwise we continue to check next objects
- }
- else{
- if(key===tgtKey){
- if(tgtObj[key]===referMap[key]){
- ansMap[key] = "Matched"
- return true;
- }
- else{
- ansMap[key] = "Not Matched"
- return false;
- }
- }
- }
- }
- // console.log(`done`)
- }
- }
- for(key in referMap){
- find(target,key)
- }
- for(key in referMap){
- if(ansMap[key]){
- console.log(`"${key}"`,ansMap[key])
- }
- else{
- console.log(`"${key}" not found`)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement