Store a replica into an array

In my database, there is a function that adds ledger entries. Entries that meet specific criteria are saved to an array. However, when it comes to saving records in the 'if' block, only the second part gets saved successfully. How can I modify the code to save both types of ledgers from the 'if' block into the array?

 //Function for pushing the ledger
 .......
 for (let i = 0; i < myledger.length; i++) {
     if (myledger[i].type === 'test' || myledger[i].type === 'Security' 
        || myledger[i].type === 'Books'){

            myledger.push(i)
    }



 if(status === active){
      record {
         type: "Books",
         Fee: 3000
      },
      record {
         type: "Security",
         Fee: 1000
      },
   }
   else {
      record {
         type: "test",
         Fee: 10000
      }
   }

Answer №1

Your issue lies in the multi-object assignment you attempted. The error is caused by trying to add two objects simultaneously, resulting in the second object overlapping the first one.

Consider using a simple array method instead.

records = [
    {
        category: "Books",
        cost: 3000
    },
    {
        category: "Security",
        cost: 1000
    }
];

Answer №2

It seems that the issue is located here:

  item {
     category: "Novels",
     Price: 2500
  },
  item {
     category: "Tech Gadgets",
     Price: 500
  },

It appears you are attempting to insert each of these items into an 'items' array, but a mistake in syntax has been made. Your current approach seems to be defining an object property named 'item' - since it is defined twice, the second item property is replacing the first.

A recommended correction could look like this:

items.push(
    {
        category: "Novels",
        Price: 2500
    },
    {
        category: "Tech Gadgets",
        Price: 500
    }
);

The specific implementation may vary based on your intended outcome. I hope this provides some clarity.

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

One of the great features of Next.js is its ability to easily change

At the moment, my dynamic path is configured to display events by their ID [id].js localhost:3000/event/1 But I would like it to be structured as follows: localhost:3000/city/date/title. All of this information is available in the events database, but I&a ...

Facing a challenge in configuring MongoDB automatic data expiration based on specific time zones

I am currently facing an issue with clearing data in MongoDB at the start of each day. For example, on July 15, 2020 at 00:00:00, data is deleted from the database based on a specific time. I am having trouble properly assigning the expiresAt attribute in ...

Saving the retrieved data from a JQuery $.post request into a JavaScript global variable

Currently utilizing Javascript and JQuery. A declaration of a Variable var RoleID=""; is stationed outside all functions. There exists a function: role_submit(){ var role=$('#emp_role').val(); var url="submitrole.php"; $.post(url, {role2: rol ...

Unable to establish a new pathway in the index.js file of a Node.js and Express website running on Heroku

I recently made some changes to my index.js file: const express = require('express'); const path = require('path'); const generatePassword = require('password-generator'); const fetch = require('node-fetch'); const ...

Extract data from Markit On Demand API using JavaScript and AJAX

I'm struggling to properly parse the response from the API. While I can retrieve the entire response, I am a bit lost on how to effectively parse it. Below is my code snippet: <!DOCTYPE> <html> <head> <style> img ...

Addressing Browser Incompatibility in React.js Event Handling

I've recently embarked on my journey to learn React.js by delving into various tutorials and documentation. However, I'm encountering a peculiar issue specifically in Google Chrome: https://i.sstatic.net/qODiP.png Interestingly, in Firefox, it ...

executing a JavaScript function in a separate .js document

When working with the success callback function in my AJAX post, I encountered an issue trying to call a function from another JavaScript file. Within page1.html: <head> <link href="style.css" rel="stylesheet" type="text/css" /> <s ...

Using the OR Operator with a different function in React

Struggling with setting the day flexibility using disableDate(1,2,3,4,0) but it's not functioning as expected. Can you assist me in fixing this issue? Here is the function snippet: const disableDate = (date) => { const day = date.day(); retur ...

Embedding Array into Mongodb is an efficient way to store and

Whenever I attempt to store array data within MongoDB using the query below, it always shows a success message without actually storing any data in an empty array inside MongoDB. My goal is to successfully store array data inside MongoDB as shown in the f ...

What's the process for converting offsetX and offsetY pixel coordinates to percentages?

Currently, I am working on a project where I need the offsetX and offsetY coordinates to be displayed in percentage (%) format while hovering over a div element. By default, these coordinates are shown in pixels. Here is an example of the structure: < ...

how can a select dropdown be dynamically displayed based on the previous selection?

If the first dropdown is set to "Professor" I want to display a second dropdown, but if it is set to "Student" then I do not want to display the second dropdown. function checkPrivilege() { var privilege = document.getElementById("permisija5").value; ...

Geometry of a Wireframe Cube

After upgrading from r59 to r62, I couldn't help but notice that the wireframe CubeGeometry now displays an extra diagonal line on each face. Is there a solution to this issue? volumeGeometry = new THREE.CubeGeometry(w, h, depth); volumeMaterial = ne ...

Angular 7's URL updates, but no other actions take place

I am new to posting here and feeling quite desperate. Currently, I am working with Angular 7 and facing an issue. The problem arises when I manually enter the desired URL, everything works perfectly. However, when I use RouterLink by clicking a button, the ...

Creating a Jasmine test for the event.target.click can be accomplished by defining a spec that

I need help creating a Jasmine test spec for the following method in my component. Here is my Component Method methodName(event): void { event.preventDefault(); event.target.click(); } I have started writing a test but don't fully cover event. ...

Retrieve information from Google Sheets to use in SVG map

While working on a local HTML page, I encountered an issue with my script. I am using the svgMap library to create a map of movies I have seen, pulling data from a Google Sheets document using the opensheet library. The JSON output looks like this: [{"Coun ...

Tips on adding additional values to a table column in Vue3 using Element-Plus

My database contains an array of objects with a 'sex' property that is either 1 for male or 2 for female. How can I use the el-table library to convert these values to display as 'male' and 'female'? I am unfamiliar with this ...

Deprecated message received from the body-parser node module

Currently learning Node.js, I've been utilizing the 'express' framework and installed body-parser successfully. However, upon starting my app, I encountered this message from Node: body-parser deprecated bodyParser: use individual json/urle ...

php code to paginate mysql results

Imagine I have 50 rows in my database. How can I retrieve MySQL results in pages, displaying 5 results on each page and showcasing the pages as follows: [1], 2, 3, 4...10? For example, if it's on page 5, show 3, 4, [5], 6, 7...10 without refreshing al ...

Combining Express and React for seamless email sending functionality

Attempting to merge a React.js form with a backend setup using Express to send emails. Uncertain of the proper way to format the form body or which HTTP request method to utilize. React.js and Express.js are located in separate directories. express-mailer ...

Exploring the power of hierarchical organization in node.js modules

One of my modules is called UserProvider and it has the following structure: var UserProvider = function(db) { ... } UserProvider.prototype.createUser = function(email, password, callback) { ... } UserProvider.prototype.findUserByEmail = function(email, c ...