0

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;
        };

3
  • You can convert the single object into an array before you do your comparison. However, the comparison might be a bit more tricky. Do you need to compare all fields in the object to decide whether they're the same? Commented Nov 9, 2021 at 12:19
  • Read this Commented Nov 9, 2021 at 12:25
  • Is areaId a unique identification of the object, or can several objects have the same areaId? Commented Nov 9, 2021 at 12:26

3 Answers 3

1

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));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Your answer was perfect.
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;
};

5 Comments

It was sample data of newArea. it can be single object or arrays of data.
This function can be simplified to: const selectedAreaMatch = (selectedArea, newArea) => newArea.some(a => a.areaId === selectedArea.areaId)
If it's either an object or an array, you'd do const selectedAreaMatch = (selectedArea, newArea) => [newArea].flat().some(a => a.areaId === selectedArea.areaId)
@CherryDT I know that it can be simplified, but I like to stick with the original code, so the requester can follow the idea, but thanks for the suggestion.
@grisuu Right, sorry I missed that there was already code like this in the question.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.