Home > database >  Find the first match between 2 objects
Find the first match between 2 objects

Time:12-01

I have 2 objects

const obj1 = {
    item1: {
        name: '1'
    },
    item2: {
        name: '2'
    }
}

const obj2 = {
    item1: {
        sample: 'sample1'
    },
    item2: {
        sample: 'sample2'
    }
}

How can I loop over the second object obj2, find the first match against object obj1 and return it?

In above example, I should return

{
    item1: {
        name: '1'
    }
}

since item1 is the first match between the 2 objects and I want what's inside obj1.

Tried the following:

const keys = obj2 && Object.keys(obj2);

const output = () => {
    if (keys) {
        const finalResponse = keys.map(key => {
          if (obj1[key]) return obj1[key];
          return undefined;
        });
        return finalResponse
    }
    return null
}

But ends up getting 2 matches when I only want the first time it matches.

Is there a cleaner way to do this.

Fine to loop over obj1 or obj2 as long as I can return the match in obj1.

CodePudding user response:

You already have the solution, you can save the key that was matched in a variable when the match exists.

const keys = obj2 && Object.keys(obj2);
let keyMatched = '';
const output = () => {
    if (keys) {
        const finalResponse = keys.map(key => {
          if (obj1[key] && !keyMatched) {   // Check for first match  
              keyMatched = key;   // Save the first match
              return obj1[key];
          }
          return undefined;
        });
        return finalResponse
    }
    return null
}

Demo run

const obj1 = {
  item1: {
      name: '1'
  },
  item2: {
      name: '2'
  }
}

const obj2 = {
  item1: {
      sample: 'sample1'
  },
  item2: {
      sample: 'sample2'
  }
}
const keys = obj2 && Object.keys(obj2);
let keyMatched = '';
const output = () => {
    if (keys) {
        const finalResponse = keys.map(key => {
          if (obj1[key] && !keyMatched) {   // Check for first match  
              keyMatched = key;   // Save the first match
              return obj1[key];
          }
          return undefined;
        });
        return finalResponse
    }
    return null
}
output();
const newObj = {
    [keyMatched]: obj1[keyMatched]
}
console.log(newObj)

CodePudding user response:

const obj1 = {
  item1: {
    name: '1'
  },
  item2: {
    name: '2'
  }
}

const obj2 = {
  item1: {
    sample: 'sample1'
  },
  item2: {
    sample: 'sample2'
  }
}

const obj1Keys = Object.keys(obj1)
const obj2Keys = Object.keys(obj2)


let match = null;

obj2Keys.every((key) => {
  if (obj1Keys.includes(key)) {
    match = obj1[key]
    return false
  }
  return true
})

console.log(match)

CodePudding user response:

With this, find the obj1 keys that are in obj2...

const object2keys = new Set(Object.keys(obj2));
const intersection = Object.keys(obj1).filter(key => object2keys.has(key));

With this, use the first (arbitrary) intersecting key to build an object with the matching key/value pair from obj1...

const match = intersection.length ? intersection[0] : null;
const result = match ? { [match] : obj1[match] } : {}

Demo...

const obj1 = {
  item1: {
    name: '1'
  },
  item2: {
    name: '2'
  }
}

const obj2 = {
  item1: {
    sample: 'sample1'
  },
  item2: {
    sample: 'sample2'
  }
}

const object2keys = new Set(Object.keys(obj2));
const intersection = Object.keys(obj1).filter(key => object2keys.has(key));
const match = intersection.length ? intersection[0] : null;
const result = match ? { [match] : obj1[match] } : {}

console.log(result);

CodePudding user response:

var result = Object.keys(obj1).find(prop=>obj2.hasOwnProperty(prop))

You can also use result to build Objekt

var resObj = {}
resObj[result] = obj2[result]
  • Related