Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on your comment, perhaps <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" rel="nofollow"><code>Array#push</code></a> is what you want:</p> <pre><code>segments = []; //foreach point in some set // compute cord1x, cord1y segments.push({x: cord1x, y: cord1y}); </code></pre> <p>Then <code>segment</code> becomes an array of objects, each representing one 2d point, consistently with your example of <code>segments = [{x: 121, y: 446}, {x: 164, y: 384}, {x: 190, y: 271}, {x: 186, y:198}, {x: 180, y:60}]</code></p> <hr> <p>Based on the question alone:</p> <p>With <code>segments = [{x: cord1x, y: cord1y}];</code>, <code>segments</code> becomes an array containing one anonymous object. <code>cord1x</code> becomes accessible as <code>segments[0].x</code> and <code>cord2x</code> becomes accessible as <code>segments[0].y</code>.</p> <p>With <code>segments = {x: cord1x, y: cord1y}</code>, <code>segments</code> becomes an object with the properties <code>x</code> and <code>y</code>. <code>cord1</code> becomes accessible as <code>segments.x</code> and <code>cord2</code> becomes accessible as <code>segments.y</code>.</p> <p>With <code>segments = [cord1x, cord1y]</code>, <code>segments</code> becomes an array of two integers. <code>cord1x</code> becomes accessible as <code>segments[0]</code> and <code>cord1y</code> becomes accessible as <code>segments[1]</code>.</p> <p>With <code>segments = '{x: '+cord1x+', y: '+cord1y+'}'</code>, <code>segments</code> becomes a string in the form <code>{x:121, y:446}</code>. The downside is that <code>cord1x</code> and <code>cord1y</code> are not as easy to retreive. The upside is that <code>segments</code> can now be compared for equality, instead of identity, by using <code>===</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload