0

Following are the code snippet for the javascript map and join. Here I want to check the object properties and if it contains the undefined then display "" and if the properties contains the zero then display 0. Code is already running but when I see output into the console I did not get the value for undefined and 0. Could some let me know what is the problem into the running code here.

let userInfo = {
  "l_idReference": "205000",
  "l_Name": undefined,
  "l_totalDebitsAch": 1,
  "l_totalCreditsAch": 0,
  "l_totalCreditAmountAch": 0,
  "l_creationDateAndTime": "2019-10-11T00:00:00"
}
checkNullOrZero = (obj) => {
    //console.log(obj ,"OBJ")
    const output =  Object.keys(obj).map(col => {
      if(obj[col] === "" || obj[col] === 0){
        obj[col] = 0
      }
      return `<td>${obj[col] ? obj[col] : ""}</td>`
      
    }).join("")
    console.log(output)
  }
  
checkNullOrZero(userInfo)

Thanks.

1
  • obj[col] ? obj[col] : "" will turn any falsy value into an empty string. Since 0 is falsy, it would also be converted into nothing. Commented Nov 9, 2019 at 8:19

2 Answers 2

1

'obj[col] ?' Condition will be false when the obj[col] is equal to zero,

So you should change your condition.

Try this :

  let userInfo = {
      "l_idReference": "205000",
      "l_Name": undefined,
      "l_totalDebitsAch": 1,
      "l_totalCreditsAch": 0,
      "l_totalCreditAmountAch": 0,
      "l_creationDateAndTime": "2019-10-11T00:00:00"
    }
    checkNullOrZero = (obj) => {
        //console.log(obj ,"OBJ")
        const output =  Object.keys(obj).map(col => {
          if(obj[col] === "" || obj[col] === 0){
            obj[col] = 0
          }
          return `<td>${obj[col]!= undefined ? obj[col] : obj[col] == 0 ? "0" : ""}</td>`
          
        }).join("")
        console.log(output)
      }
      
    checkNullOrZero(userInfo)

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

1 Comment

The code may be correct, but please add an explanation of how it solves the problem, so that the op can learn from it
0

Here you go. Just check for the type undefined of the property.

userInfo = {
  "l_idReference": "205000",
  "l_Name": undefined,
  "l_totalDebitsAch": 1,
  "l_totalCreditsAch": 0,
  "l_totalCreditAmountAch": 0,
  "l_creationDateAndTime": "2019-10-11T00:00:00"
}
checkNullOrZero = (obj) => {
  //     console.log(obj ,"OBJ")
  const output = Object.keys(obj).map(col => {
    if (obj[col] === "" || obj[col] === 0) {
      obj[col] = 0
    }
    if (obj[col] === undefined) {
      obj[col] = `""`
    }
    return `<td>${obj[col]}</td>`

  }).join("")
  console.log(output)
}

checkNullOrZero(userInfo)

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.