var list = [
    { date: '12/1/2011', reading: 3, id: 20055 },
    { date: '13/1/2011', reading: 5, id: 20053 },
    { date: '14/1/2011', reading: 6, id: 45652 }
];

and then access it:

alert(list[1].date);
Answer from Darin Dimitrov on Stack Overflow
๐ŸŒ
DEV Community
dev.to โ€บ hcoco1 โ€บ javascript-dynamic-list-the-dom-manipulation-10c5
JavaScript Dynamic List: The DOM Manipulation. - DEV Community
May 16, 2023 - It also creates a delete button for each item using the button element. The delete button is assigned the text "Delete" and an event listener that triggers the deleteItem() function with the corresponding index when clicked. Also, it is appended as a child of the li element, and the li element is appended as a child of the ul element. This process is repeated for each item in the list array, resulting in a dynamically generated list of countries with corresponding delete buttons.
Top answer
1 of 1
3

Here is a small example that I think might help you. This creates objs with dynamic names and values and adds them to an array.

var myArray = [];
for (var i = 0; i < 3; i++) {
  var myObj = {};
  for (var x = 0; x < 3; x++) {
    myObj["Field" + x] = "val" + x;
  }
  myArray.push(myObj);
}
console.log(myArray)

Edit after Op's edit

Here is another example after your update. If I understood you then I think you really only need 1 function that is essentially an update to the array. Your JSON was a bit off too I think. In the example below The ID will reference an object that has a fields array where your "list" will be stored.

var myArray = [];
var testObj = {};
testObj.id = 1;
testObj.fields = [];
myArray.push(testObj)

// add myValue to the already existing obj with ID 1
addValue(1, "myValue");
// add myValue2 to the non-existent id 2 obj (it will be dynamically created)
addValue(2, "myValue2");

function addValue(id, val){
  var foundID = false;
  myArray.forEach(function(obj){
   if(obj.id === id){
     var newObj = {};
     newObj["Field" + id] = val;
     obj.fields.push(newObj);
     foundID = true;
   }
  });
  // if we did not find an obj with the passed in ID
  // then create and initialize it
  if(!foundID){
    var newIDObj = {};
    newIDObj.id = id;
    var fieldsArray = [];
    var newFieldsObj = {};
    newFieldsObj["Field" + id] = val;
    fieldsArray.push(newFieldsObj);
    newIDObj.fields = fieldsArray;
    myArray.push(newIDObj);
  }
}
console.log(myArray);

// How to access a value knowing the ID and the fieldName
var result = getValue(1, "Field1");

// If no result is found, undefined will be returned
// This assumes Field Names are all unique!
function getValue(id, fieldName) {
  var result;
  myArray.forEach(function(currentObj) {
    if (currentObj.id === id) {
      currentObj.fields.forEach(function(currentField) {
        if (typeof currentField[fieldName] !== "undefined") {
          result = currentField[fieldName];
        }
      });
    }
  });
  
  console.log("The value with ID: " + id + " and field name: " + fieldName + " is: " + result)
  return result;
}

๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-create-a-dynamic-HTML-list-with-Javascript.php
How to Create a Dynamic HTML List with Javascript
Javascript adds functionality so that if a user clicks the 'Add Item' button, an additional item is added to the list. The code to do this is shown below. So remember that we have an HTML that has 3 items already in the list. Therefore, we start the counter at 4. ... Remember that this function is triggered when the 'Add Element' button is clicked. So we create a variable, completelist, that gets the element with an id of "thelist"
๐ŸŒ
KIRUPA
kirupa.com โ€บ html5 โ€บ dynamically_create_populate_list.htm
Dynamically Create a List : Frontend Coding Exercises
Show off your DOM skills in this fun exercise where you get to dynamically generate a list.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-create-dynamic-values-and-objects-in-javascript
How to Create Dynamic Values and Objects in JavaScript? | GeeksforGeeks
September 2, 2024 - In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values ...
Top answer
1 of 2
3

This will depend on your content and how you prepare it, but I'd suggest a very generic solution that'll work for any level of nested points. Now, arbitrary nesting doesn't appear to be required in your case, but, hey, nice to have.

Suppose your JSON content is structured like so:

var points = [
    {title: "Point", children: [
        {title: "Point"},
        {title: "Point"},
        {title: "Point"},
        {title: "Point", children: [
            {title: "Point"},
            {title: "Point"},
            {title: "Point", children: [
              // more...?
            ]}
        ]}
    ]}
]

You'll note that you can just keep nesting the points indefinitely.

To render this as HTML, you can use a recursive function. Like this:

function buildList(parentElement, items) {
    var i, l, list, li;
    if( !items || !items.length ) { return; } // return here if there are no items to render
    list = $("<ul></ul>").appendTo(parentElement); // create a list element within the parent element
    for(i = 0, l = items.length ; i < l ; i++) {
        li = $("<li></li>").text(items[i].title);  // make a list item element
        buildList(li, items[i].children);          // add its subpoints
        list.append(li);
    }
}

And call it like so:

buildList($("#pageContent").empty(), points);

The point is that since the structure is recursive, it can nest to any depth, but the code is simpler.

Here's a jsfiddle. This is quite a different approach, and Daniel Cook's answer is probably more immediately applicable to you current code, but I thought it worth to point out.

You could also extend it to add the "1", "1.1", "1.2" (and so on) numbers to the titles.

2 of 2
2

You do not need to add the id to the created li if you store it to a variable.

The code below shows the basic concept. Does it make you more comfortable?

$.each(current.contents, function(_, mp){
    var $li = $('<li>' + mp.main + '</li>');       
    if (mp.subPoints.length){
        var $ul =$('<ul>')
        $.each(mp.subPoints, function(_, sp){
            $ul.append('<li>' + sp + '</li>');
        });
        $li.append($ul);
    }
    $pageContent.append($li);
});

Here's a fiddle using the different coding I'm sure this could be improved more. I'm also still learning.

๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Generate a List from an Array Dynamically Using JavaScript | Display Array Items Easily in JS - YouTube
Want to turn an array into a dynamic HTML list using JavaScript? In this beginner-friendly tutorial, youโ€™ll learn how to generate a list from an array dynami...
Published: July 11, 2025
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 46768471 โ€บ javascript-create-array-of-objects-dynamically
Javascript create array of objects dynamically - Stack Overflow
Option 1: Use Array#reduce to collect the name and translation of each object to a new object: Copyconst data = [{"name":"title","value":"STEP-01","translation":"STEP-01"},{"name":"meta_description","value":"","translation":"meta desc"}]; const ...
๐ŸŒ
daily.dev
daily.dev โ€บ home โ€บ blog โ€บ webdev โ€บ js create array of objects: simplified
JS Create Array of Objects: Simplified | daily.dev
May 25, 2026 - I hope this guide has helped you ... To make an array of objects in JavaScript, just use square brackets [] and put your objects, surrounded by curly braces {}, inside....
Top answer
1 of 1
2

I recommend putting the script at the end of the page, just before </body> The reason is that you'll want to have scripts run when the DOM is ready (i.e. all the elements are present and ready for JS manipulation).

<center> is a deprecated stylistic element. To center an element, set left and right margins to auto. For text, use text-align: center:

.centered-element {
  display: block;
  width: 250px; /* Your desired width */
  margin-left: auto;
  margin-right: auto;
}

.element-with-centered-text {
  text-align: center;
}

This way, you have separation of concerns. HTML deals with describing the structure, CSS deals with styling your HTML.

* {
    font-family: "Times New Roman", Times, serif;
}

This is unnecessary. Applying font-family to body should be enough to apply it to all elements. Font style is inherited by all descendants until an element overrides it (and styles its own descendants).

fieldset {...}

input[type=text] {...}

button {...}

ul {..}

li {...}

I don't recommend styling elements directly because it breaks expectations. If I add a <ul> in your app for whatever purpose, I expect it to have bullets. But since your CSS removes them, my list won't have bullets. I'd have to add them back when, by default, they should have had bullets.

Instead, use classes to target app-specific styling and leave element defaults alone. The only time I would style an element directly is if it's part of a globally-applied theme or a normalizer.

As for your JavaScript, you could abstract away your element creation with a function like the following. This way, you don't have to repeat element creation scripts everywhere and you can easily describe dynamic HTML in a nested manner, like HTML.

const e = (name, properties = {}, children = []) => {
  // Create the element
  const element = document.createElement(name)

  // Apply properties
  Object.keys(properties).forEach(property => {
    element.setAttribute(property, propertyes[property])
  })

  // Append children
  children.forEach(c => {
    if(!c) return
    const node = (typeof c === 'string') ? document.createTextNode(c) : c
    element.appendChild(node)
  })

  return element
}

// Usage
const root = e('div', {}, [
  e('p', {}, [
    e('span', {}, [
      'Hello, World!',
      'Lorem Ipsum'
    ])
  ])
])

root.appendChild(e('span', {}, ['another piece of text']))

If the syntax looks familiar, it's because this is the basic premise of how most VDOM libraries work under the hood. They're just nested calls of functions that either return an actual element, or objects that represent elements (which are turned to elements later).

From there, you can describe and build your "components" like:

const LoadForm = () => {
  e('div', {}, [
    e('input', { type: 'text', name: 'user_input', placeholder: 'Enter here...' }),
    e('br'),
    e('button', { name: 'add_list' }, ['Add To List']),
    e('button', { name: 'remove_list' }, ['Remove From List']),
  ])

document.getElementById('in').appendChild(LoadForm())

You can even encapsulate this in a class if you want so that you can also add some helper methods.

class LoadForm {
  onAdd () {
    // Do something on add
  }
  onRemove () {
    // Do something on remove
  }
  render() {
    return (
      e('div', {}, [
        e('input', { type: 'text', name: 'user_input', placeholder: 'Enter here...' }),
        e('br'),
        e('button', { name: 'add_list', onclick: this.onAdd }, [
          'Add To List'
        ]),
        e('button', { name: 'remove_list', onclick: this.onRemove }, [
          'Remove From List'
        ]),
      ])
    )
  }
}

document.getElementById('in').appendChild((new LoadForm()).render())

Lastly, JavaScript is written in camelCase, not snake_case.

๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ list of objects in javascript
How to Create a List of Objects in JavaScript | Delft Stack
February 2, 2024 - ... const object = { // object's different member's names & values name1: value1, name2: value2, name3: value3 }; ... The value of an object member can be a number, a string, an array, or even a function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-creating-html-list-from-javascript-array
How to create HTML List from JavaScript Array? - GeeksforGeeks
Example: In this example, we create an HTML list dynamically from a JavaScript array using a for loop.
Published: 3 weeks ago