-1
var status = result.locations[index].status;
var operator = result.locations[index].operator;
var original = result.locations[index].original;
var produced = result.locations[index].produced;
var href = result.locations[index].more;

I have the above which each need to be an if statement to check if there is content and my output is the below code.

if (result.locations[index] && result.locations[index].status){
    var status = result.locations[index].status;
} else {
    var status = '';
}

I would need to reproduce this per line from the code at the top of the post. What would be the best method to simplify each down to keep the code neater and not produce 5 lines of if statement when 1 or 2 would do.

benomatis
  • 5,536
  • 7
  • 36
  • 59
ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • 2
    What's wrong with several lines ? There is no 'elvis' operator in JS, like in Groovy, if that's what you're searching... – phtrivier Oct 14 '14 at 15:27
  • 2
    don't see the reason for downvote, it's a proper question... – benomatis Oct 14 '14 at 15:28
  • @webeno I suppose the downvotes are because we don't know *why* the OP would want to reduce it to one line. I suspect he is looking for a less verbose way to look "deep" inside an object... – phtrivier Oct 14 '14 at 15:29
  • @webeno haters gonna hate, I like cleaner code and at the moment I have 6 lines like `var status = result.locations[index].status` that need to be changed into an if statement like OP so I wanted to shorten down the code. To clarify: I never down voted – ngplayground Oct 14 '14 at 15:30
  • 1
    @phtrivier i don't get it, it's a simple question that has a simple answer to it... i don't think the OP needs to give a reason for one like this... – benomatis Oct 14 '14 at 15:31
  • @Donald i wish i could eliminate your downvotes :) i helped with one upvote, that's all i can do for now... – benomatis Oct 14 '14 at 15:32
  • If you have multiple calls create a helper `TryReturnValueOr(result.locations[index], '')` – Alex K. Oct 14 '14 at 15:32
  • @AlexK. I will update the original post then – ngplayground Oct 14 '14 at 15:33
  • I don't think it's a good question because it's not clear what's being asked here that would not have been answered by any trivial 'write if statement on one line' google search. For example, what does the "with 2 requirements" part have to do with it? Also, do you actually want one line, or one statement? Edit: It's much better now. – Boann Oct 14 '14 at 15:34
  • I have updated the original post to summarise WHY I needed to keep it minimal :) – ngplayground Oct 14 '14 at 15:36
  • @Donald think our answer with tymeJV still stand following your update, you just have to replace the relevant bits for each line... – benomatis Oct 14 '14 at 15:38

4 Answers4

2
var status = (result.locations[index] && result.locations[index].status ? result.locations[index].status : '');
benomatis
  • 5,536
  • 7
  • 36
  • 59
1

Not sure why you want to, but:

var status = (result.locations[index] && result.locations[index].status) ? result.locations[index].status : ""
tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

Your problem is trying to access a property of a "deep" javascript object using its path.

This is a common question :

Javascript: Get deep value from object by passing path to it as string

Accessing nested JavaScript objects with string key

There is no built-in way to do this in javascript.

There are plenty of libraries to do that, for example, with selectn, this would become something like (I have not tested it, so I don't know if the index part will work, but you get the idea) :

var status = selectn("locations." + index + ".status", result) || ''

If the structure of your objects is always the one above (that is, the property is just at one level of depth), and you're not expecting 'falsy', you could simply write the 'test' function yourself :

function safeGet(instance, propertyName, defaultValue) {
    // As pointed by AlexK, this will not work 
    // if instance[propertyName] can be anything Falsy ("", 0, etc...)
    // If it's possible, get a library that will do 
    // the full series of insane checks for you ;)
    if (instance && instance[propertyName)) {
      return instance[propertyName];
    } else {
      return defaultValue;
    }
}

var location = result.locations[index]; // Potentially undefined, but safeGet will deal with it

var status = safeGet(location, "status", "");
var operator = safeGet(location, "operator", "DEFAULT_OPERATOR");
...
Community
  • 1
  • 1
phtrivier
  • 13,047
  • 6
  • 48
  • 79
0
var status = result.locations[index] && result.locations[index].status || ''; 

However, better maje sure before, if result.locations[index] exists... else do whatever is to be done in your code..

fast
  • 885
  • 7
  • 15