1

I try to push the rank value inside the array if it is not empty. But i cannot check the empty value.

Array Data 
0: {name: 0, age: 58, dob: "", mark1: 63, mark2: 43}
1: " "
2: " "
3: " "
4: " "
5: " "
6: " "

if object is empty it should not enter into if loop,right now Loops enter into all the 6 objects.

for (let j = 0; j <= 6; j++) 
    {

        if(rankData[j] !==undefined &&  rankData[j] !==" " &&   (Object.keys(rankData[j]).length)!== 0 &&
        rankData[j] !=='undefined' &&  rankData[j] !=='undefined-undefined' )
        { 
            rankData[j].rank = J;
        }
    }

If object is not empty is should push the rank value . But right now even object is empty it enter into if loop and show error in this line rankData[j].rank = J;

4
  • What is the rank value in your case? Check if the array has an empty string? It's hard to understand what you need, please reformulate Commented Apr 2, 2020 at 9:40
  • The loop will happen every time, are you saying the code inside the if is run every time? (Also, you have a typo, raknData) Commented Apr 2, 2020 at 9:40
  • @DBS : yes,even empty object it goes inside the loop. Commented Apr 2, 2020 at 9:46
  • JS and Typescript are case sensitive languages, did you mean rankData[j].rank = j;? Also, if you see errors, please include them in the question, they are generally the most important piece of information about a problem. Commented Apr 2, 2020 at 9:49

3 Answers 3

1

It is necessary to check whether element of an array satisfies the condition. If the condition is satisfied, then assign some value to rank property:

let arr = [
    { name: 0, age: 58, dob: "", mark1: 63, mark2: 43 }
    , " "
    , " "
    , " "
    , " "
    , " "
    , " "
];

for (let index = 0; index < arr.length; index++) {    
    let elem = arr[index];
    if (Object.keys(elem).length > 0 && typeof elem != 'string' && elem) {
        elem.rank = index;
        console.log(elem);
    }
}

console.log(arr);

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

Comments

0

If you want to check weather some given “something” Is empty you can use this function that I once found on internet.

function isEmpty (value) {
   if (typeof value === “undefined”) return true;
   if (value === null) return true;
   if (typeof value === “string” && value.trim().length === 0) return true;
    if (Array.isArray(value).length === 0) return true;
   if (typeof value === “object” && Object.keys(value).length === 0) return true

  // if none of that is true we return false 
    return false;
}

Comments

0

I think you have used J instead of j (in rankData[j].rank = J;)

var array = [
  {name: 0, dob: ""},
  " ",
  " ",
  " "
];

for(var j=0; j<=4; j++){
if(array[j] !== undefined && array[j] !== " ")
        {
            array[j].rank = j;
        }
}

console.log(array);

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.