Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
JavaScript check Array and object match

I have one selected store object data. When I make one POST request as return I get single object of data or arrays of data, it depends on postal code. One postal code have multiple of PickUp points or single PickUp point.

I want to create an Helper function based on the POST request return data, If it does not match with my already selected store object data then return true else return else. I don't know how to create helper function which is unpredictable, it can be single object or arrays with multiple object.

Here is my sample data. I want to compare the data with areaId

    const selectedArea = {
          areaId: "84ed84ad-81e3-49ed-absjsjjjs",
          name: "New york restaurant",

          address: {
            city: "New York",
            postalCode: "0012231",
            street: "New jersy"
          }
        };

        const newArea = [
          {
            areaId: "84ed84ad-81e3-49ed-absyay6w",

            name: "Veg retaurant",

            address: {
              city: "Calfornia",
              postalCode: "0018383",
              street: "Calfornia"
            }
          },
          {
            areaId: "84ed84ad-sjsjjs",

            name: "Indian restaurant",

            address: {
              city: "Calfornia",
              postalCode: "001aas8383",
              street: "Calfornia"
            }
          },
          {
            areaId: "84ed84ad-81e3-49sjsjjshq8q",

            name: "Desi restaurant",

            address: {
              city: "Calfornia",
              postalCode: "0011881",
              street: "Calfornia"
            }
          },
          {
            areaId: "84ed84ad-sjsjj-ssks-msms",

            name: "chinese restaurant",

            address: {
              city: "Calfornia",
              postalCode: "02992",
              street: "Calfornia"
            }
          }
        ];
        
        const newArea = 
          {
            areaId: "84ed84ad-81e3-49ed-absyay6w",

            name: "Veg retaurant",

            address: {
              city: "Calfornia",
              postalCode: "0018383",
              street: "Calfornia"
            }
          }
        ;

         const selectedAreaMatch = (selectedArea, newArea) => {
          console.log({ selectedArea });
          console.log({ newArea }); // it can be object or arrays

          // if does not match return true
          // if match return false
          return true;
        };

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can overcome the difference by converting the argument to an array using [].concat. The nice thing of this method is that it does not matter whether the argument is an array or not, it will lead to the same result.

It is strange that you expect true when there is no match, yet call the function selectedAreaMatch. I would then call it selectedAreaIsNew, or else invert the return value, so that it is true when there is a match. However, I kept the name and expected return value as you specified:

const selectedAreaMatch = (selectedArea, newArea) => {
     // if does not match return true
     // if match return false
     return ![].concat(newArea).some(area => area.areaId === selectedArea.areaId);
};

const selectedArea = {areaId: "84ed84ad-81e3-49ed-absjsjjjs",name: "New york restaurant",address: {city: "New York",postalCode: "0012231",street: "New jersy"}};

const newArea = [{areaId: "84ed84ad-81e3-49ed-absyay6w",name: "Veg retaurant",address: {city: "Calfornia",postalCode: "0018383",street: "Calfornia"}},{areaId: "84ed84ad-sjsjjs",name: "Indian restaurant",address: {city: "Calfornia",postalCode: "001aas8383",street: "Calfornia"}},{areaId: "84ed84ad-81e3-49sjsjjshq8q",name: "Desi restaurant",address: {city: "Calfornia",postalCode: "0011881",street: "Calfornia"}},{areaId: "84ed84ad-sjsjj-ssks-msms",name: "chinese restaurant",address: {city: "Calfornia",postalCode: "02992",street: "Calfornia"}}];
        
const newArea2 ={areaId: "84ed84ad-81e3-49ed-absyay6w",name: "Veg retaurant",address: {city: "Calfornia",postalCode: "0018383",street: "Calfornia"}};

console.log(selectedAreaMatch(selectedArea, newArea));
console.log(selectedAreaMatch(selectedArea, newArea2));

about 4 years ago · Juan Pablo Isaza Report

0

First of all newArea has been declared twice. If you'd like to compare the two areas by their areaId, you can do in your function:

   const selectedAreaMatch = (selectedArea, newArea) => {
    console.log({selectedArea});
    console.log({newArea}); // it can be object or arrays

    // if does not match return true
    // if match return false

    for (let i = 0; i < newArea.length; i++) {
        if (newArea[i].areaId === selectedArea.areaId) {
            return true;
        }
    }
    return false;
};
about 4 years ago · Juan Pablo Isaza Report

0

You can convert the object into an array with a single element and then compare:

const selectedAreaMatch = (selectedArea, newArea) => {
    const areas = Array.isArray(newArea) ? newArea : [newArea];

    // assumes that the area ids are unique
    return areas.some(area => area.areaId === selectedArea.areaId)
};

The comparison is a little more complicated if the area ids are not unique. In that case you'd need to iterate over each property of the object and compare with that of the other.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!