0

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.

3
  • 1
    Is that inside a function? If not, what are you trying to return from? Commented Oct 12, 2021 at 10:43
  • You are looking for break, not return btw Commented Oct 12, 2021 at 10:44
  • @Tvde1, sorry my bad it should be break. Commented Oct 12, 2021 at 10:45

4 Answers 4

1

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

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

Comments

1

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

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

Comments

0

In you code you can replace return from break so it will go out from loop once condition satisfices.

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

use break to step out from loop.

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.