0

I use this in a loop:

 $(".somediv").append('text' + array[key].arrayelement + 'sometext'+ array[key].nextelementinArray + 'moretext']);

How do I implement this: if array[key].arrayelement is null (yeah, it can be only text 'null'), then do one thing, and if notm, then another thing?

I tried this appendable text to make 2 variables, but it did not work.

card.append('<div class="col-sm-12 col-md-6 col-lg-4"><div class="info-block bg-grey m-b-2 p-a-2"><h4 class="h4 underline-sm"> ' + response[key].firstname + ' ' + response[key].lastname + ' '  + '</h4><div class="col-sm-8 text-xs-left">                        <p class="m-b-1"><b>'+ response[key].job_title + '<br>Tööruum:' + $.trim(response[key].room) + '</b></p><a href="mailto:' + $.trim(response[key].email) + '">' + $.trim(response[key].email) + '</a><p class="m-b-0">' + $.trim(response[key].phone) + ' ' + $.trim(response[key].cell_phone) + '</div><div class=" col-sm-4 text-xs-right m-b-1"><img src="' + $.trim(response[key].image) + '" class="rounded-circle lg" alt=""></div></div></div>');

Simple i get: But i dont want to see

Tööruum: ' + response[key].room

when the room value is text 'null'. I tried this inside append, where is this response[key] room.

`function(){if(response[key].room===null) {return " ";}else {return '<br>Tööruum: + response[key].room';}};

but nada.

3
  • By saying "only text 'null'" you really mean a String containing null like "null"? Commented Sep 17, 2017 at 13:57
  • text null. Sample of JSON i get. `[{"id":"1333","contract_id":"2735","firstname":"Margit","lastname":"Ader","ended_school":"Taertu Kutsehariduskeskus" ,"ending_year":"2008","ended_proffession":"Pagar, kondiiter","department_id":"17","department":"Toitlustus" ,"phone":null,"cell_phone":null,"image":null,"email":null,"education":null,"address":null,"room":null ,"user_id":"18774","job_title":"m\u00fc\u00fcja"},{"id":"1495","contract_id":"647","firstname":"Margit" ,"lastname":"Miil","ended_school":"Suure-Jaani Keskkool","ending_year":"1977","ended_proffession":null ,"department_id":"40","departmen Commented Sep 17, 2017 at 14:54
  • well that means it's really type null , not a string Commented Sep 17, 2017 at 14:59

4 Answers 4

2

You could use a Ternary Operator, and wrap your statement and resolution into () brackets to isolate the logic:

$(".somediv").append('text' + (array[key].arrayelement == 'null' ? <one thing> : <other thing>) + 'sometext'+ array[key].nextelementinArray + 'moretext']);
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a callback, and do any number of things

$(".somediv").append(function() {
    if ( array[key].nextelementinArray === null ) {
         return "something";
    } else if ( array[key].nextelementinArray ) {
         return "something else";
    } else {
         return "fail";
    }
});

Comments

0

Just appending based on condition

if (array[key] && array[key] !== 'null') {
    $(".somediv")append('text' + array[key].arrayelement + 'sometext'+ array[key].nextelementinArray + 'moretext']);
}

Comments

-1

Maybe you could solve this by ternary operator? Something similar to:

(array[key].arrayelement == null ? 'some backup value' : array[key].arrayelement)

More "javascripty-way" would look like:

(array[key].arrayelement || 'some backup value')

If you want to do something more complicated, you could use inline function:

...'text' + (function(){
             //do whatever logic you want
             //return string to append
             return "my uber string";
           })() + //rest of your concatenation

2 Comments

Why would you use an IIFE instead of simply () (your first example which is BTW already suggested...) - Your second example is probably not the desired since it's just used to test a boolean...
With IIFE you can do more complicated logic like declaring a help variable... that's not possible with simple (), like: var res = ( var mySubResult = "hello; mySubResult + "end of text" );//cant do this And as to the second example - that's a perfectly fine solution. It's not testing boolean but testing for null/0/false... (elvis? anyone?), like: var res = (object.thisIsNull || "backup value"); //ress will be "backup value"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.