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


Groups > comp.lang.javascript > #30163 > unrolled thread

convert a string into a json object,

Started byJRough <janis.rough@gmail.com>
First post2016-03-31 19:12 -0700
Last post2016-04-02 04:25 +0200
Articles 20 — 7 participants

Back to article view | Back to comp.lang.javascript


Contents

  convert a string into a json object, JRough <janis.rough@gmail.com> - 2016-03-31 19:12 -0700
    Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-01 10:42 +0200
      Re: convert a string into a json object, JRough <janis.rough@gmail.com> - 2016-04-01 17:32 -0700
    Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-01 10:49 +0200
      Re: convert a string into a json object, John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-01 19:21 +0100
        Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-01 16:21 -0300
        Re: convert a string into a json object, Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-01 20:36 +0100
          Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 04:07 +0200
            Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-02 12:07 -0300
              Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 17:36 +0200
                Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-02 21:01 -0300
            Re: convert a string into a json object, Scott Sauyet <scott@sauyet.com> - 2016-04-02 20:40 +0000
      Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-02 01:18 +0200
        Re: convert a string into a json object, John Harris <niam@jghnorth.org.uk.invalid> - 2016-04-02 11:29 +0100
      Re: convert a string into a json object, Scott Sauyet <scott@sauyet.com> - 2016-04-02 20:30 +0000
    Re: convert a string into a json object, JRough <janis.rough@gmail.com> - 2016-04-01 11:32 -0700
      Re: convert a string into a json object, Stefan Weiss <krewecherl@gmail.com> - 2016-04-02 00:51 +0200
        Re: convert a string into a json object, Aleksandro <aleksandro@gmx.com> - 2016-04-02 12:03 -0300
    Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 03:48 +0200
    Re: convert a string into a json object, Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 04:25 +0200

#30163 — convert a string into a json object,

FromJRough <janis.rough@gmail.com>
Date2016-03-31 19:12 -0700
Subjectconvert a string into a json object,
Message-ID<c8134afb-b7f5-4e2d-b3e6-90f8a3594544@googlegroups.com>
It does the first split  into a 3 dim arr but it doesn't split those 3 strings up into the data for the jsonObject.  Thanks.

<!DOCTYPE html>
<head>
<script> 
var string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';
function doJson(string){
var arr = string.split("\n");
   for (i=0;i< 2;i++){
        var  str=  arr[i].split("_");
            var jsonObj= {};
                jsonObj.name =str[0];
                jsonObj.city= str[1];
                jsonObj.job = str[2];
  
    }
    return jsonObj;
 }

   
    

var myObj = doJson(string);
console.log(myObj);
</script>
</head>
<body>

</body>

</html>

[toc] | [next] | [standalone]


#30165

FromStefan Weiss <krewecherl@gmail.com>
Date2016-04-01 10:42 +0200
Message-ID<ndlc9h$9tf$1@news.albasani.net>
In reply to#30163
JRough wrote:
> var string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';

The line delimiter in this string is '"\n"' instead of '\n' - is that
intentional?

> function doJson(string){
> var arr = string.split("\n");

split() should use the same delimiter as the input: '"\n"'.

>    for (i=0;i< 2;i++){

The variable i needs to be declared. And the code is more flexible if
the number of records isn't hard-coded. Speaking of which, the string
contains three records, but you're only counting: zero, one.

>         var  str=  arr[i].split("_");
>             var jsonObj= {};
>                 jsonObj.name =str[0];
>                 jsonObj.city= str[1];
>                 jsonObj.job = str[2];

This is ok, but can be writen in a more compact way:

    var jsonObj = {
        name: str[0],
        city: str[1],
        job:  str[2],
    };

>     }
>     return jsonObj;

This will return the last assembled object. You probably want a list of
all those objects.

>  }
> 
> var myObj = doJson(string);


You seem to have a misunderstanding about what JSON is: it's a simple
string format for data serialization. What you're building here are
JavaScript objects, not JSON strings.


Here is a version which avoids the mentioned problems:

  // I'll assume the line delimiter is actually \n
  var string='Janis_SF_WebDev\nDebby_SJ_WebDev\nStephanie_Fl_Recruite\n';

  function parseString (string)
  {
      var lines = string.split("\n");
      var result = [];  // this array is new, it holds all your objects

      for (var i = 0; i < lines.length; i++) {
          if (!lines[i].length) {
              continue; // skips empty records, like the last one
          }
          var str = lines[i].split("_");
          var obj= {
              name: str[0],
              city: str[1],
              job:  str[2],
          };
          result.push(obj);  // add the object to the result
      }

      return result;  // returns the array instead of a single object
  }

  // this converts the input string into an array of objects
  var myList = parseString(string);

  // this produces a JSON string from what that list
  var json = JSON.stringify(myList);


- stefan

[toc] | [prev] | [next] | [standalone]


#30177

FromJRough <janis.rough@gmail.com>
Date2016-04-01 17:32 -0700
Message-ID<1da1333c-1a79-4b96-a33e-46699b3eeee4@googlegroups.com>
In reply to#30165
On Friday, April 1, 2016 at 1:42:36 AM UTC-7, Stefan Weiss wrote:
okay, thanks,  for the new line char having single quotes, guess I was thinking of strings where you can interchange it instead of ASCII.
> JRough wrote:
> > var string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';
> 
> The line delimiter in this string is '"\n"' instead of '\n' - is that
> intentional?
> 
> > function doJson(string){
> > var arr = string.split("\n");
> 
> split() should use the same delimiter as the input: '"\n"'.
> 
> >    for (i=0;i< 2;i++){
> 
> The variable i needs to be declared.
tnx,
 And the code is more flexible if
> the number of records isn't hard-coded. Speaking of which, the string
> contains three records, but you're only counting: zero, one.

thanks ;-)   guess I was thinking the count was the same as the index starting at 0.
> 
> >         var  str=  arr[i].split("_");
> >             var jsonObj= {};
> >                 jsonObj.name =str[0];
> >                 jsonObj.city= str[1];
> >                 jsonObj.job = str[2];
> 
> This is ok, but can be writen in a more compact way:
> 
>     var jsonObj = {
>         name: str[0],
>         city: str[1],
>         job:  str[2],
>     };
> 
> >     }
> >     return jsonObj;
> 
> This will return the last assembled object. You probably want a list of
> all those objects.
> 
> >  }
> > 
> > var myObj = doJson(string);
> 
> 
> You seem to have a misunderstanding about what JSON is: it's a simple
> string format for data serialization. What you're building here are
> JavaScript objects, not JSON strings.

so this is a object literal?  the requirement was to build a json object.
> 
> Here is a version which avoids the mentioned problems:
> 
>   // I'll assume the line delimiter is actually \n
>   var string='Janis_SF_WebDev\nDebby_SJ_WebDev\nStephanie_Fl_Recruite\n';
> 
>   function parseString (string)
>   {
>       var lines = string.split("\n");
>       var result = [];  // this array is new, it holds all your objects
> 
>       for (var i = 0; i < lines.length; i++) {
>           if (!lines[i].length) {
>               continue; // skips empty records, like the last one
>           }
>           var str = lines[i].split("_");
>           var obj= {
>               name: str[0],
>               city: str[1],
>               job:  str[2],
>           };
>           result.push(obj);  // add the object to the result
>       }
> 
>       return result;  // returns the array instead of a single object
>   }
> 
>   // this converts the input string into an array of objects
>   var myList = parseString(string);
so if I get you right,  then from the object literal you produce the Json object with the JSON stringify.   Nice.  THanks, I think I get it.  
>   // this produces a JSON string from what that list
>   var json = JSON.stringify(myList);
> 
> 
> - stefan

thanks,  nice to be out of that one

[toc] | [prev] | [next] | [standalone]


#30166

FromStefan Weiss <krewecherl@gmail.com>
Date2016-04-01 10:49 +0200
Message-ID<ndlcms$ae5$1@news.albasani.net>
In reply to#30163
Stefan Ram wrote:
> JRough <janis.rough@gmail.com> writes:
>> var string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';
> 
> main = function self()

Why not just `function main()`?

> { "use strict";
>   const result = {};

Why an object instead of an array as container?

>   const string = 'Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';
>   const row = string.split( '"\n"' );
>   for( let i = 0; i < 3; ++i )

Hard-coding number of records...

>   { const record = row[ i ].split( "_" );
>     const person = {};
>     person.name = record[ 0 ];
>     person.city = record[ 1 ];
>     person.job  = record[ 2 ];
>     result[ i ]= person; }
>   console.log( JSON.stringify( result ));
>   return result; };
> 
> main();
> 
>  "{"0":{"name":"Janis","city":"SF","job":"WebDev"},"1":{"name":"Debby","city":
>"SJ","job":"WebDev"},"2":{"name":"Stephanie","city":"Fl","job":"Recruite"}}"


Off-topic... does anymody else find it counter-intuitive when most of the
variables are declared as "const"? I've seen this style recently in many
places, and it's technically correct, but for some reason I find it irritating.


- stefan

[toc] | [prev] | [next] | [standalone]


#30170

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-04-01 19:21 +0100
Message-ID<i0ftfbt8uvgeeg9nsn9hqk8496tmn647pp@4ax.com>
In reply to#30166
On 1 Apr 2016 16:45:59 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:

>Stefan Weiss <krewecherl@gmail.com> writes:  
  <snip>
>>Off-topic... does anymody else find it counter-intuitive when most of the
>>variables are declared as "const"? I've seen this style recently in many
>>places, and it's technically correct, but for some reason I find it irritating.
>
>  These /are/ constants because they are not assigned to after
>  their initializations. I deem it most readable to declare
>  constants with »const« and not with »let«. Because it /is/
>  a constant, it also should be /declared/ to be a constant.

The code contained
  const result = {};
and
  result[i]= person;

The constant  result  is not what I would call a constant. You've
updated it.

  John

[toc] | [prev] | [next] | [standalone]


#30172

FromAleksandro <aleksandro@gmx.com>
Date2016-04-01 16:21 -0300
Message-ID<ndmhi3$kv2$1@dont-email.me>
In reply to#30170
On 01/04/16 15:21, John Harris wrote:
> On 1 Apr 2016 16:45:59 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:
> 
>> Stefan Weiss <krewecherl@gmail.com> writes:  
>   <snip>
>>> Off-topic... does anymody else find it counter-intuitive when most of the
>>> variables are declared as "const"? I've seen this style recently in many
>>> places, and it's technically correct, but for some reason I find it irritating.
>>
>>  These /are/ constants because they are not assigned to after
>>  their initializations. I deem it most readable to declare
>>  constants with »const« and not with »let«. Because it /is/
>>  a constant, it also should be /declared/ to be a constant.
> 
> The code contained
>   const result = {};
> and
>   result[i]= person;
> 
> The constant  result  is not what I would call a constant. You've
> updated it.

Constant is the object it references, it's address, the pointer,
whatever underlies.

[toc] | [prev] | [next] | [standalone]


#30173

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-04-01 20:36 +0100
Message-ID<87d1q9qfyr.fsf@bsb.me.uk>
In reply to#30170
John Harris <niam@jghnorth.org.uk.invalid> writes:

> On 1 Apr 2016 16:45:59 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:
>
>>Stefan Weiss <krewecherl@gmail.com> writes:  
>   <snip>
>>>Off-topic... does anymody else find it counter-intuitive when most of the
>>>variables are declared as "const"? I've seen this style recently in many
>>>places, and it's technically correct, but for some reason I find it irritating.
>>
>>  These /are/ constants because they are not assigned to after
>>  their initializations. I deem it most readable to declare
>>  constants with »const« and not with »let«. Because it /is/
>>  a constant, it also should be /declared/ to be a constant.
>
> The code contained
>   const result = {};
> and
>   result[i]= person;
>
> The constant  result  is not what I would call a constant. You've
> updated it.

const makes the binding immutable, the object isn't.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#30182

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-04-02 04:07 +0200
Message-ID<2542125.XVB1Yskezl@PointedEars.de>
In reply to#30173
Ben Bacarisse wrote:

> John Harris <niam@jghnorth.org.uk.invalid> writes:
>> The code contained
>>   const result = {};
>> and
>>   result[i]= person;
>>
>> The constant  result  is not what I would call a constant. You've
>> updated it.
> 
> const makes the binding immutable, the object isn't.

Neither is the value as it is updated on each iteration.  This code should 
break; ECMAScript got it wrong.

Rule of thumb of good programming: Do not write code in a way that is
harder to understand.
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#30189

FromAleksandro <aleksandro@gmx.com>
Date2016-04-02 12:07 -0300
Message-ID<ndon26$gbn$1@dont-email.me>
In reply to#30182
On 01/04/16 23:07, Thomas 'PointedEars' Lahn wrote:
> Ben Bacarisse wrote:
> 
>> John Harris <niam@jghnorth.org.uk.invalid> writes:
>>> The code contained
>>>   const result = {};
>>> and
>>>   result[i]= person;
>>>
>>> The constant  result  is not what I would call a constant. You've
>>> updated it.
>>
>> const makes the binding immutable, the object isn't.
> 
> Neither is the value as it is updated on each iteration.  This code should 
> break; ECMAScript got it wrong.
> 
> Rule of thumb of good programming: Do not write code in a way that is
> harder to understand.

I don't think we will change our way to code because you don't understand.

[toc] | [prev] | [next] | [standalone]


#30190

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-04-02 17:36 +0200
Message-ID<1630971.JlSNffjCh4@PointedEars.de>
In reply to#30189
Aleksandro wrote:

> On 01/04/16 23:07, Thomas 'PointedEars' Lahn wrote:
>> Ben Bacarisse wrote:
>>> John Harris <niam@jghnorth.org.uk.invalid> writes:
>>>> The code contained
>>>>   const result = {};
>>>> and
>>>>   result[i]= person;
>>>>
>>>> The constant  result  is not what I would call a constant. You've
>>>> updated it.
>>> const makes the binding immutable, the object isn't.
>> Neither is the value as it is updated on each iteration.  This code
>> should break; ECMAScript got it wrong.
>> 
>> Rule of thumb of good programming: Do not write code in a way that is
>> harder to understand.
> 
> I don't think we will change our way to code because you don't understand.
                ^^
You must be referring to yourself and the voices in that troll head of 
yours.

Straw man.  I am *obviously* not the only one who thinks that this code is 
written in a counter-intuitive way, and should therefore be written 
differently.

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

[toc] | [prev] | [next] | [standalone]


#30196

FromAleksandro <aleksandro@gmx.com>
Date2016-04-02 21:01 -0300
Message-ID<ndpmal$2pr$1@dont-email.me>
In reply to#30190
On 02/04/16 12:36, Thomas 'PointedEars' Lahn wrote:
> Aleksandro wrote:
> 
>> On 01/04/16 23:07, Thomas 'PointedEars' Lahn wrote:
>>> Ben Bacarisse wrote:
>>>> John Harris <niam@jghnorth.org.uk.invalid> writes:
>>>>> The code contained
>>>>>   const result = {};
>>>>> and
>>>>>   result[i]= person;
>>>>>
>>>>> The constant  result  is not what I would call a constant. You've
>>>>> updated it.
>>>> const makes the binding immutable, the object isn't.
>>> Neither is the value as it is updated on each iteration.  This code
>>> should break; ECMAScript got it wrong.
>>>
>>> Rule of thumb of good programming: Do not write code in a way that is
>>> harder to understand.
>>
>> I don't think we will change our way to code because you don't understand.
>                 ^^
> You must be referring to yourself and the voices in that troll head of 
> yours.
> 
> Straw man.  I am *obviously* not the only one who thinks that this code is 
> written in a counter-intuitive way, and should therefore be written 
> differently.

Someone else agreeing with you doesn't make you automatically right.
Calling a troll someone who doesn't agree with you neither does.

By the way, since you ignored my question last time, I ask again: why
did you “unplonk” me? ;)

[toc] | [prev] | [next] | [standalone]


#30195

FromScott Sauyet <scott@sauyet.com>
Date2016-04-02 20:40 +0000
Message-ID<ndpan8$gce$2@dont-email.me>
In reply to#30182
Thomas 'PointedEars' Lahn wrote:
> Ben Bacarisse wrote:
>> John Harris wrote:

>>> The constant  result  is not what I would call a constant. You've
>>> updated it.
>> 
>> const makes the binding immutable, the object isn't.
> 
> Neither is the value as it is updated on each iteration.  This code
> should break; ECMAScript got it wrong.

I believe ECMAScript got the name wrong.  They used the `const` name from 
C, in which it has always led to a great deal of confusion.

But the fact that it can change on every iteration is not an issue.  
That's the whole point of block-level scoping.  It does not contradict 
the central notion of this as an immutable reference.

  -- Scott

[toc] | [prev] | [next] | [standalone]


#30175

FromStefan Weiss <krewecherl@gmail.com>
Date2016-04-02 01:18 +0200
Message-ID<ndmvkt$8lq$1@news.albasani.net>
In reply to#30166
Stefan Ram wrote:
> Stefan Weiss <krewecherl@gmail.com> writes:
>>> for( let i = 0; i < 3; ++i )
>> Hard-coding number of records...
> 
>   What needs to be hard-coded depends on the specification of
>   the problem. In the case of this thread, no other
>   specification than the source code was given. The source
>   code contained this /fixed/ string constant »'Janis...«.
>   There also is YAGNI and 
>   c2.com/cgi-bin/wiki?DoTheSimplestThingThatCouldPossiblyWork.

YAGNI is my secret Achilles heel :/
I'm always trying to read between the lines and anticipate the actual
requirements instead of what was explicitly asked. I've written and deleted
thousands of lines of code for this reason.

>> Off-topic... does anymody else find it counter-intuitive when most of the
>> variables are declared as "const"? I've seen this style recently in many
>> places, and it's technically correct, but for some reason I find it irritating.
> 
>   These /are/ constants because they are not assigned to after
>   their initializations. I deem it most readable to declare
>   constants with »const« and not with »let«. Because it /is/
>   a constant, it also should be /declared/ to be a constant.

I know, they are technically constants.
But the semantics seem off to me... I can't really explain this properly. In
other languages, I would use `const` or `#define` (etc) to indicate a value
that will be used literally throughout the rest of the program. Any
reference to the constant can be replaced by its value. Declaring a `const`
only for the duration of a single loop iteration just doesn't seem "right".


- stefan

[toc] | [prev] | [next] | [standalone]


#30187

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-04-02 11:29 +0100
Message-ID<6m7vfb9isd72iluou2ohes8v2ph48t10po@4ax.com>
In reply to#30175
On 2 Apr 2016 01:51:45 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:

  <snip>
>  In a few words, I'd say that one can read »const x = ...« as
>  a promise that one will not later write »x =« (as the start
>  of an expression) in the same block.
  <snip>

Yes, but what was the point of making that promise here? You are
building an object with lots of own-properties, starting from {}.
Is it likely that you might replace the object with a Date object by
mistake? Is it unlikely that you will want to re-initialise the
variable by assigning {} to it instead of deleting properties?

I agree with Stefan Weiss and Thomas here : it gives the wrong
impression in cases like this.

  John

[toc] | [prev] | [next] | [standalone]


#30194

FromScott Sauyet <scott@sauyet.com>
Date2016-04-02 20:30 +0000
Message-ID<ndpa4e$gce$1@dont-email.me>
In reply to#30166
Stefan Weiss wrote:

> Off-topic... does anymody else find it counter-intuitive when most of
> the variables are declared as "const"? I've seen this style recently in
> many places, and it's technically correct, but for some reason I find it
> irritating.

No, I don't find it so any longer.  I did for a time, though.  I think it 
was the years of working in Javascript with only function-level scoping 
(well, and global, of course) that makes it feel really wrong to see 
something listed as `const` in a repeated block-scope.

But I've grown to like it.  So long as I don't try to think of it as the 
definition of a constant (which can be tempting) it works well.

I like the distinction between:

    let myMutableRef = -1, i = 0;
    while(i < something) {
      myMutableRef = someFunc(i);
      doSomething(myMutableRef);
      i++;
    }

and

    let i = 0;
    while (i < something) {
      const myImmutableRef = someFunc(i);
      doSomething(myImmutableRef);
      i++;
    }

While they perform the same function, the latter makes it clear that the 
variable does not get reassigned inside the body of the while statement.  
The former -- or any version based on `var` -- would not make it so clear.

  -- Scott

[toc] | [prev] | [next] | [standalone]


#30171

FromJRough <janis.rough@gmail.com>
Date2016-04-01 11:32 -0700
Message-ID<906e4754-7a87-4f89-9b85-4068ac79f79a@googlegroups.com>
In reply to#30163
I struggled with it and I got this part but the result is obj: webdev???  kind of a weird result but maybe I don't know how to display an object in js.
function doJson(string){
var arr = string.split("\n");
   for (i=0;i< 2;i++){
        var  str=  arr[i].split("_");
            var jsonObj= {};
                jsonObj= { "name": str[0]};
                jsonObj= { "city": str[1]};
                jsonObj = { "job": str[2]};
  
    }
    return jsonObj;
 }


the point of the exercise was take a string and make it a json object.  I guess the fastest way to do it is fine.



On Thursday, March 31, 2016 at 7:12:31 PM UTC-7, JRough wrote:
> It does the first split  into a 3 dim arr but it doesn't split those 3 strings up into the data for the jsonObject.  Thanks.
> 
> <!DOCTYPE html>
> <head>
> <script> 
> var string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';
> function doJson(string){
> var arr = string.split("\n");
>    for (i=0;i< 2;i++){
>         var  str=  arr[i].split("_");
>             var jsonObj= {};
>                 jsonObj.name =str[0];
>                 jsonObj.city= str[1];
>                 jsonObj.job = str[2];
>   
>     }
>     return jsonObj;
>  }
> 
>    
>     
> 
> var myObj = doJson(string);
> console.log(myObj);
> </script>
> </head>
> <body>
> 
> </body>
> 
> </html>

[toc] | [prev] | [next] | [standalone]


#30174

FromStefan Weiss <krewecherl@gmail.com>
Date2016-04-02 00:51 +0200
Message-ID<ndmu0v$67n$1@news.albasani.net>
In reply to#30171
JRough wrote:
> I struggled with it and I got this part but the result is obj: webdev???
> kind of a weird result but maybe I don't know how to display an object in js.
[...]
>             var jsonObj= {};
>                 jsonObj= { "name": str[0]};
>                 jsonObj= { "city": str[1]};
>                 jsonObj = { "job": str[2]};

You assign {} to jsonObj, and then you assign three other things to it. This
cannot possibly work. Please read Stefan Ram's and my reply and try to work
our suggestions into your code.


- stefan

[toc] | [prev] | [next] | [standalone]


#30188

FromAleksandro <aleksandro@gmx.com>
Date2016-04-02 12:03 -0300
Message-ID<ndomp4$do6$1@dont-email.me>
In reply to#30174
On 01/04/16 19:51, Stefan Weiss wrote:
> JRough wrote:
>> I struggled with it and I got this part but the result is obj: webdev???
>> kind of a weird result but maybe I don't know how to display an object in js.
> [...]
>>             var jsonObj= {};
>>                 jsonObj= { "name": str[0]};
>>                 jsonObj= { "city": str[1]};
>>                 jsonObj = { "job": str[2]};
> 
> You assign {} to jsonObj, and then you assign three other things to it. This
> cannot possibly work. Please read Stefan Ram's and my reply and try to work
> our suggestions into your code.

To clear mental ambiguities; jsonObj's value is replaced three times.

[toc] | [prev] | [next] | [standalone]


#30180

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-04-02 03:48 +0200
Message-ID<3785004.pgkuUax1YM@PointedEars.de>
In reply to#30163
Stefan Ram wrote:

> JRough <janis.rough@gmail.com> writes:
>>var
>>string='Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"';
> 
> main = function self()

Unwise.

  1. Undeclared identifier: main.  Breaks in strict mode.
  2. Used/confusing/unnecessary name: self.
       - Used/confusing:
           * There is window.self;
           * the name breaks local code with unqualified references to
             “self” written under the assumption that it refers to
             the same object as “window.self”.
       - Unnecessary: In your code, “self” is not even used.  There is no
         recursion, no self() call.
           * Older environments create a global “self” variable.

> { "use strict";
>   const result = {};
    ^^^^^
>   const string =
>   'Janis_SF_WebDev"\n"Debby_SJ_WebDev"\n"Stephanie_Fl_Recruite"\n"'; const
>   row = string.split( '"\n"' ); for( let i = 0; i < 3; ++i )
                                       ^^^
>   { const record = row[ i ].split( "_" );
>     const person = {};

Unnecessary incompatibility.

>     person.name = record[ 0 ];

If the value is at least conceptionally *constant*, how come it is 
*modified* to contain a new property?

If the identifier is conceptionally *a* *constant*, how come it is assigned 
a reference to a *new* object on each iteration?

> […]
>     person.city = record[ 1 ];
>     person.job  = record[ 2 ];
>     result[ i ]= person; }
> 
>   console.log( JSON.stringify( result ));
>   return result; };
>  
> main();

As usual, your code, including your code style, is, unnecessarily,
a maintenance nightmare.

Ever heard of the module pattern and other environments than the latest 
Firefox?

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

[toc] | [prev] | [next] | [standalone]


#30183

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-04-02 04:25 +0200
Message-ID<2854466.X3ZI4lkZKL@PointedEars.de>
In reply to#30163
Stefan Ram wrote:

>   { const record = row[ i ].split( "_" );
>     const person = {};
>     person.name = record[ 0 ];
>     person.city = record[ 1 ];
>     person.job  = record[ 2 ];

Destructuring assignment:

  [person.name, person.city, person.job] = record;

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment>

<http://www.ecma-international.org/ecma-262/6.0/#sec-destructuring-assignment>

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web