title: Javascript creating json object from two strings
body: <p>So I will start with my needs. I have a task to create json output using nightwatch.js from the ul list where inside lists are few div elements with classes like name, surname... But really I can't think of any of solutions. Here is my html</p>
  
  <pre><code>    &lt;html&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;body&gt;
  &lt;ul class="random"&gt;
      &lt;li class="list"&gt;
          &lt;div class="name"&gt;John&lt;/div&gt;
          &lt;div class="surname"&gt;Lewis&lt;/div&gt;
      &lt;/li&gt;
  
      &lt;li class="list odd"&gt;
          &lt;div class="name"&gt;Nick&lt;/div&gt;
          &lt;div class="surname"&gt;Kyrgios&lt;/div&gt;
      &lt;/li&gt;
  
  &lt;/ul&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is my nightwatch.js script</p>
  
  <pre><code>'Test' : function(browser) {
  
      function iterate(elements) {
        elements.value.forEach(function(el) {
          browser.elementIdText(el.ELEMENT, function(r) {
  
           browser.elementIdAttribute(el.ELEMENT, 'class', function(att){
  
            // output for json i guess
  
             console.log(att.value + ' =&gt; ' + r.value) 
  
           })
  
          });
        });
      }
  
      browser
        .url('http://url.com/nightwatch.php')
        .waitForElementVisible('body', 8000)
        .elements('css selector', 'ul li div', iterate)
        .end();
    }
  </code></pre>
  
  <p>Basically this will execute the following:</p>
  
  <ol>
  <li>name => John</li>
  <li>surname => Lewis</li>
  <li>name => Nick</li>
  <li>surname => Kyrgios</li>
  </ol>
  
  <p>Output is a string for both...</p>
  
  <p>And how can I make it like</p>
  
  <pre><code>[{name: "John", surname: "Lewis"}, {name: "Nick", surname: "Kyrgios"}]
  </code></pre>