Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda