title: beginner how to combine code together Javascript
body: <p>Hi I just started learning JS. I want to put cart1 and cart2 together.</p>
  <pre><code>
  if (cart1) {
          for (const key in data) {
            main.push({
              id: Math.random().toString(),
              name: data[key].name
              image: data[key].image
              location: data[key].location,
            });
          }
        }
  
  if (cart2) {
          for (const key in data) {
            main.push({
              id: Math.random().toString(),
              name: data[key].name
              image: data[key].image
            });
          }
        }
  
      
  </code></pre>
  <p>I used ternary operator, but I don't want cart2 have <code>location</code> property</p>
  <pre><code>if (cart2 || cart1) {
          for (const key in data) {
            main.push({
              id: Math.random().toString(),
              name: data[key].name
              image: data[key].image
              location: cart1? data[key].location : &quot;&quot;,
            });
          }
        }
  </code></pre>
  <pre><code></code></pre>