Guide to appending two strings into an array and showcasing them in a vertical format

Looking to add two strings to an array and display them one below the other:

const str1 = "SamsungGalaxy A52"
const str2 = "SamsungGalaxy A53"

let arr=[]

arr.push(str1,str2)

This will output as follows:

  SamsungGalaxy A52
  SamsungGalaxy A53

Answer №1

If you wish to add two values to an array, you have the option of using arr.push(str1, str2); or arr.push(str1); arr.push(str2);

To showcase this list in the user interface, you can create a ul & li list.

const str1 = "SamsungGalaxy A52"
const str2 = "SamsungGalaxy A53"

let arr=[]

arr.push(str1);
arr.push(str2);
// arr.push(str1, str2);

console.log(arr);

const ul = document.createElement('ul');

for (const item of arr) {
  const li = document.createElement('li');
  li.textContent = item;
  ul.append(li);
}

document.querySelector('body').appendChild(ul)

Answer №2

If you're looking to create a simple product list display, you might want to consider the following code snippet:

const product_1 = "SamsungGalaxy A52";
const product_2 = "SamsungGalaxy A53";

let products = [];

products.push(product_1, product_2);

const ul = document.querySelector('ul');

for (const product of products) {
  const li = document.createElement('li');
  li.textContent = product;
  ul.append(li);
}
li {
  list-style: none;
}
<ul>
</ul>

Answer №3

One way to create a string and add it as the inner HTML is by:

const item_1 = "iPhone X";
const item_2 = "iPhone 11";

const items = [];
items.push(item_1, item_2);

// or simply
// const items = [item_1, item_2];

document.querySelector("#store").innerHTML = items
  .map((item) => `<div>${item}</div>`)
  .join("");
#store {
  margin-left: 20px;
}
<div id="store"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing data automatically in PHP through radio buttons

As a beginner in PHP, I am working with 2 radio buttons where one is checked by default. When the page loads, I want to display content based on the default selected radio button. ...

Javascript Error: Object Expected

Recently, I have encountered an error in my code that says "object expected" in JavaScript. Surprisingly, the code was working perfectly fine before this issue arose. Strangely, the code is still functioning properly in another solution. Even after making ...

Can you explain the purpose of this function on Google PlusOne?

Within the code snippet below: (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByT ...

Modifying an HTML attribute dynamically in D3 by evaluating its existing value

I'm facing a seemingly simple task, but I can't quite crack it. My web page showcases multiple bar graphs, and I'm aiming to create a button that reveals or conceals certain bars. Specifically, when toggled, I want half of the bars to vanish ...

Issue with displaying the Bootstrap-select DropDownList

Within my web application, I have a bootstrap-select combobox that I populate with data using an ajax call. Html: <div class="row"> <select id="selectGemeente1" class="selectpicker" data-live-search="true"></select> </div> Ajax ca ...

Unable to connect HTML data to the view section using angular.js

I am trying to incorporate some HTML content into my view using angular.js. time.html: <table class="table table-bordered table-striped table-hover" id="dataTable"> <tr> <td width="100" align="center">Time <i class="fa fa-long- ...

Issues encountered during the deployment process on Vercel

I've encountered an issue while deploying my Next.js app on Vercel. It seems to be related to an unsupported platform error. I have attached an image displaying the exact error message. npm ERR! code EBADPLATFORM npm ERR! notsup Unsupported platform f ...

Develop a regular expression control specifically designed to validate URLs

I encountered a issue with my current web application. When I access the link: https://localhost:44311/#shop, the page loads perfectly as expected. Here is a screenshot: https://i.sstatic.net/6G6CJ.png However, when I try to change the URL to: https://loc ...

I am unable to achieve negative X degree rotation of the image while using mousemove to rotate it

I need assistance with moving a picture in 3D. I want the offsetX of the mouse to be positive when it's past half of the picture, and negative otherwise. How can I achieve this effect for rotation degrees? This is what I have tried: $('#img ...

I am encountering a problem with validating my form using JavaScript

I've been working on a project where I'm developing a website using JavaScript, HTML, and CSS to search for movies and purchase tickets. While the basic functionalities are working fine, I'm encountering some challenges with form validation. ...

What could be the reason for this JSON being considered "invalid"?

Despite passing validation on jsonlint, both Firefox and Chrome are rejecting this JSON: { "messages": [ { "subject": "One" }, { "subject": "Two" }, { "subject": "Three" ...

Angular view fails to update after form submission when using ngDialog to change the scope

After starting my Angular journey, I decided to challenge myself by creating a comprehensive todo app for educational purposes. I seem to be missing something pretty basic, although I can't quite put my finger on it. It seems like there might be an is ...

How to use jQuery to remove a class from the last entered data in a form

Reminder: This is a jQuery coding challenge, and I am required to write the validation script without using any plugins or additional modules. I have created a basic form validation script. If a user inputs data that is empty, an appropriate error message ...

Retrieve the data from an array located inside an object using a try-catch block

Currently, my approach involves utilizing a try-catch block to check the accessibility of web services: try { WebServices::create($this->nameWS); } catch (Exception $e) { var_dump($e); } The variable $e now holds the err ...

Increase the Quantity of Points in a Point Cloud by Using the User's Mouse Clicks

Currently, I am utilizing Three.js to display point cloud data fetched from a server. When dealing with each data set, I iterate through the data points and construct a Vector3 object in Three.js holding x, y, and z values for each point. Then, I store th ...

Provide a unique <li> attribute for the JavaScript function to utilize

Is there a way to pass specific attributes from dropdown options to a javascript function? I have tried using .data() and .attr(), but the console keeps showing "undefined". Any suggestions on how to achieve this in a cleaner and simpler way would be gre ...

What causes TypeScript to automatically infer a default property when dynamically importing a JavaScript file that lacks a default export?

While dynamically importing a javascript file that exports multiple functions (without a default export), I encountered this issue: const sayHi = import('./sayHi.js') I was expecting the type of sayHi to be Promise<{name1: function, name2: fu ...

Issue: ParserError encountered due to a Syntax Error found at line 1, column 32

As a beginner in programming, I am encountering an issue. When I run "npm run build" in the Terminal to compress my project in React.js, I encounter this error. Interestingly, I have previously created another project without facing this problem, and I can ...

Determine the total value of a specific key within a collection of objects

I am grappling with an array of objects: let fileArray = [ { filename: 'File1.txt', bytes: 12345, created: 1548360783511.728 }, { filename: 'File2.txt', bytes: 34567, created: 1548361491237.182 }, { filename: 'File3.txt&apos ...

Is Fetch executed before or after setState is executed?

I've encountered an issue while trying to send data from the frontend (using React) to the backend (Express) via an HTML form, and subsequently clearing the fields after submission. The code snippet below illustrates what I'm facing. In this scen ...