1

Hi i have this function below that removes duplicates from the variable suggest but sometimes the data i receive for the suggest variable is empty and the code below wont work beacause of the JSON.parse function how do i make it so that the variable accepts empty data so that the code below will still run without any problems. Any help would be appreciated thanks!

function suggestData(data){

    var suggest = []; // I tried adding this and setting it to null but it doesn't seem to work.
    suggest = JSON.parse(data);

    for (var i = suggest.length - 1; i >= 0; i--) { 
        for (var j = 0; j < arrString.length; j++) {
            if (suggest[i] === arrString[j]) {
                suggest.splice(i, 1);
            }
        }
    }
1
  • So you want to check if data-parameter has value? Did you try finding it on Google or this site, there are numerous answers for that. Commented Jun 8, 2018 at 7:29

5 Answers 5

1

You can set suggest inside an if condition to check whether data exists or not

function suggestData(data){

    var suggest = [];

    if(data){
       suggest = JSON.parse(data);
    }

    for (var i = suggest.length - 1; i >= 0; i--) { 
        for (var j = 0; j < arrString.length; j++) {
            if (suggest[i] === arrString[j]) {
                suggest.splice(i, 1);
            }
        }
    }

Also you can simply end the function if you do not want to execute it when data is empty:

function suggestData(data) {
   if(!data){
      return false;
    }
  //other code here
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

Set the length conditionally as,

var length = suggest? suggest.length: 0;

function suggestData(data){

    var suggest = [];
    suggest = JSON.parse(data);
    var length = suggest? suggest.length: 0;
    for (var i = length - 1; i >= 0; i--) { 
        for (var j = 0; j < arrString.length; j++) {
            if (suggest[i] === arrString[j]) {
                suggest.splice(i, 1);
            }
        }
    }
}
suggestData(null);

3 Comments

actually i want to make it so that it can JSON.parse a empty string
@Bobby I did not get that. What do you mean?
Because this error keeps poping up SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data so im thinking that it cant run because the suggest is empty. So what i want to is to try to make the JSON.parse be able to parse a empty string
1

function suggestData(data){

var suggest = []; // I tried adding this and setting it to null but it doesn't seem to work.

if(data) suggest = JSON.parse(data);

for (var i = suggest.length - 1; i >= 0; i--) { 
    for (var j = 0; j < arrString.length; j++) {
        if (suggest[i] === arrString[j]) {
            suggest.splice(i, 1);
        }
    }
}

Just add the if condition on parsing line like written in above code::

if(data) suggest = JSON.parse(data);

Comments

1

The simplest:

if(!data || !suggest)
    return;

Comments

0

let array = [1,2,3,8,3,4,4,5]
const removeDuplicateItems = arr => [...new Set(arr)];

console.log(removeDuplicateItems(array))

If you just want to remove duplicated items in an array, you can instead try Set which is quite handy

Even if array is empty or null, this method removeDuplicateItems will still works

1 Comment

@downvoters: How is suggesting another way is consider a bad answer?

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.