[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

How to solve this ?

Ajinkya Joshi

8/19/2015 7:19:00 AM

Write a javascript function, findPath(obj, str), that takes a javascript object and a string parameter and iterates through the object and returns the path to the key that contains the string as a value (using '.' as a separator). For example:

var obj = { bat: bee, bird: plane, bee : { foo : bar, test : { text: needle } } };

var path = findPath (obj, 'needle');

Would yield:

path = "bee.test.text"
4 Answers

Martin Honnen

8/19/2015 9:29:00 AM

0

Ajinkya Joshi wrote:
> Write a javascript function, findPath(obj, str), that takes a javascript object and a string parameter and iterates through the object and returns the path to the key that contains the string as a value (using '.' as a separator). For example:
>
> var obj = { bat: bee, bird: plane, bee : { foo : bar, test : { text: needle } } };

Should needle be a string literal 'needle'? Or is that another variable
defined elsewhere?




--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

ram

8/19/2015 12:20:00 PM

0

Ajinkya Joshi <ajinkya.joshi.d@gmail.com> writes:
>Write a javascript function, findPath(obj, str), that takes a
>javascript object and a string parameter and iterates through
>the object and returns the path to the key that contains the
>string as a value (using '.' as a separator). For example:

>var obj = { bat: bee, bird: plane, bee : { foo : bar, test : { text: needle } } };
>var path = findPath (obj, 'needle');
>Would yield:
>path = "bee.test.text"

This is a quick attempt at it that prints »bee.test.text« in
the special case you gave as an example, but still contains
errors and is written in bad style. Your remaining exercise
is to find all the errors in my code and remove them and
improve the style of my code.

( function()
{ "use strict";

function PathJoin( prefix, postfix )
{ return( prefix.length > 0 ? prefix + "." : "" )+ postfix; }

function output( string )
{ console.log( string );
document.write( string ); }

function findPathImpl( path, container, value )
{ "use strict";
const
hasOwn = Object.prototype.hasOwnProperty;
var
key,
entry;
for( key in container )if( hasOwn.call( container, key ))
{ if( typeof key === "string" )
{ entry = container[ key ];
if( typeof entry === "string" )
{ if( container[ key ]=== value )return PathJoin( path, key ); }
else if( typeof entry === "object" )
return findPathImpl( PathJoin( path, key ), entry, value ); }}
return undefined; }

function findPath( container, value )
{ "use strict";
return findPathImpl( "", container, value ); }

function test()
{ "use strict";
const
obj ={ bat: 'bee', bird: 'plane', bee :{ alpha : 'beta', test :{ text: 'needle' }}},
path = findPath( obj, 'needle' );
output( path ); }

test(); }) ();

Denis McMahon

8/19/2015 5:03:00 PM

0

On Wed, 19 Aug 2015 00:19:22 -0700, Ajinkya Joshi wrote:

> Write a javascript function ....

coursework yourself do!

--
Denis McMahon, denismfmcmahon@gmail.com

Evertjan.

8/19/2015 5:27:00 PM

0

Denis McMahon <denismfmcmahon@gmail.com> wrote on 19 Aug 2015 in
comp.lang.javascript:

> On Wed, 19 Aug 2015 00:19:22 -0700, Ajinkya Joshi wrote:
>
>> Write a javascript function ....
>
> coursework yourself do!

courseoff.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)