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

235
Visualizações
Getting error while checking any one of object has some value using Javascript

I am getting error while checking any object has some value from one array using Javascript. I am explaining my code below.

let arr = [
    {
      "config": "as",
      "payload": ""
    },
    {
      "config": "as",
      "payload": "xc"
    },
    {
      "config": "",
      "payload": "xc"
    },
    {
      "config": "",
      "payload": ""
    }
]

let isNext = true;

for(let i=0; i < arr.length; i++) {
  if(arr[i].config !== '' || arr[i].payload !=='') {
     isNext = false
     return;
  }else{
     isNext = true;
  }
}

console.log('next', isNext);

Here I have one array of object and each object has 2 key-value pair. I need inside array it will check if any one of object has some value for both config or payload then isNext will be false other wise it will be true. but as per this code I am getting the following error.

Uncaught SyntaxError: Illegal return statement"

Please help me to resolve this issue.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Break the loop instead of using return, the return keyword used to return values from methods

return MDN

let arr = [
    {
      "config": "as",
      "payload": ""
    },
    {
      "config": "as",
      "payload": "xc"
    },
    {
      "config": "",
      "payload": "xc"
    },
    {
      "config": "",
      "payload": ""
    }
]

let isNext = true;

for(let i=0; i < arr.length; i++) {
  if(arr[i].config !== '' || arr[i].payload !=='') {
     isNext = false
     break;
  }else{
     isNext = true;
  }
}

console.log('next', isNext);

about 4 years ago · Juan Pablo Isaza Relatório

0

you shouldn't have a return statement outside a function scope, if you're trying to escape the for loop use break instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your code is strange because you have no result.
Maybe you want to find the first suitable object from your array?
So the code will be:

const arr = [
  {
    "config": "as",
    "payload": ""
  },
  {
    "config": "as",
    "payload": "xc"
  },
  {
    "config": "",
    "payload": "xc"
  },
  {
    "config": "",
    "payload": ""
  }
];

let result = null;
let isNext = true;

for(let i=0; i < arr.length; i++) {
  if(arr[i].config !== '' || arr[i].payload !=='') {
     isNext = false
     result = arr[i];
     break;
  }else{
     isNext = true;
  }
}

console.log('my result', result);

Or you want to check if any of elements are suitable for your condition you can make:

const result = arr.some(el => el.config || el.payload);

Or you can find and return the first suitable element:

const result = arr.find(el => el.config || el.payload);
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