Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31924

Re: Get object from one of its values

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Get object from one of its values
Date 2016-12-20 23:10 +0100
Organization PointedEars Software (PES)
Message-ID <1771227.kXSN5OTJKJ@PointedEars.de> (permalink)
References <W9ednbBeYoFENcXFnZ2dnUU7-RfNnZ2d@westnet.com.au> <2298053.9Mp67QZiUf@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:

>     if (!obj.hasOwnProperty("childArray") || obj.childArray.indexOf(5) < 0)
>     {
>       return false;
>     }
> 
>     return true;

This can be simplified to

    return (obj.hasOwnProperty("childArray")
            && obj.childArray.indexOf(5) > -1);

or

    return (obj.childArray && obj.childArray.indexOf(5) > -1);

if it does not matter that the “childArray” property is inherited.
(But it is assumed here that it holds a reference to an Array instance,
and that the Array.prototype.indexOf() method of ES 5+ is implemented.)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Get object from one of its values Andrew Poulos <ap_prog@hotmail.com> - 2016-12-20 14:43 +1100
  Re: Get object from one of its values Jake Jarvis <pig_in_shoes@yahoo.com> - 2016-12-20 10:42 +0100
    Re: Get object from one of its values Andrew Poulos <ap_prog@hotmail.com> - 2016-12-21 07:25 +1100
  Re: Get object from one of its values Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-20 13:33 +0100
    Re: Get object from one of its values Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-20 23:10 +0100
  Re: Get object from one of its values John Harris <niam@jghnorth.org.uk.invalid> - 2016-12-20 18:21 +0000

csiph-web