How can I compare data from two maps

Issue

I tried comparing data from each table from my data base but i failed
I’m not too familiar with react, I’m still working on it, I’m trying to compare the data from recommendation and customization and if they are the same I display them.

  const getRecommendation = () => {
    Axios.get("http://localhost:5000/recommendations").then((response) => {
      setRecomList(response.data);
    });
  };
  const getCostumization = () => {
    Axios.get("http://localhost:5000/customizations").then((response) => {
      setCustomList(response.data);
    });
  };
  const getRecById = async (id) => {
    Axios.get(`http://localhost:5000/recommendations/${id}`).then((res) => {
      setRecById(
        recById.filter((val) => {
          return val._id === id;
        })
      );
    });
  };
  useEffect(() => {
    {
      recommendation.map((rec, i) => {
        customization.map((cus, j) => {
          if (
            rec.type === cus.type &&
            rec.violonBody === cus.violonBody &&
            rec.violonStick === cus.violonStick &&
            rec.violonChincrest === cus.violonChincrest
          ) {
            getCostumization();
          }
        });
      });
    }
  });

Thank you!

Solution

You can use like below

const compare = (obj1, obj2) => {
  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);
  if (keys1.length !== keys2.length) return false;
  return keys1.every((key) => obj1[key] === obj2[key]);
};

console.log(compare({ a: 1, b: 2 }, { a: 1, b: 2}));
console.log(compare({ a: 1, b: 2 }, { a: 1, b: 2, c:3 }));

Answered By – Rahul Sharma

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.