utilizing the push method to update variable values within a JSON document

I am working with a json file and two variables. I need to save the values of these variables in the json file using the push function. Here's my initial code:

var x = 'xmen';
var z = 'xmen website';

var jsonObj = {
"items":
[
    {
        "title": "some title",
        "url": "some url"
    }    
]
};

The desired output in the json file should be:

var jsonObj = {
"items":
[
    {
        "title": "some title",
        "url": "some url"
    },
    {
        "title": "xmen",
        "url": "xmen website"
    }    
]
};

Instead of using arrays while pushing the value using:

jsonObj.items.push

I want to call the variable and assign it directly, like this example:

jsonObj.items.push({"title": +x + ,"url": +z + }); // This is just for demonstration purposes.

Answer №1

It appears that the problem lies not with the push method, but with your object syntax.

Instead of:

{"title":+x+,"url":+url+}

Consider using this format instead:

// Assuming that "x" and "url" are valid variables
{"title":x,"url":url}

To see a demonstration of this in action, check out this link: http://jsbin.com/zicofoye/1/edit

jsonObj.items.push({
    "title":"avengers",
    "url":"avengers website"
});

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

Unusual characters encountered in JSON data during a Volley request

While making a Json request, I encountered an issue where some of the strings with accents or special characters like 'ç' were not displaying correctly in my list view. For instance, the string 'Bragança' showed up as 'Braganç ...

Even though I have the image button's ID, I am unable to successfully click on it

I am facing an issue where I can't seem to click on an image button even though I know its ID: driver.findElement(By.id("its Id")).click() Unfortunately, I cannot provide the website link as it is not a public website. However, I have pasted the HTM ...

Show the text from the chosen option using jQuery with a button

Hey everyone, I have a question regarding a coding issue I'm facing. In this problem, I am unable to display the text from the selected option when I click the order button. The desired result is to show the selected option that I choose and display i ...

Methods for displaying a placeholder in a semantic-ui-vue dropdown without using the multiple attribute

When utilizing the dropdown feature in semantic-ui-vue, I have encountered an issue where the placeholder option does not function unless the multiple attribute is included. You can view my sandbox demonstration at this link. I have set up 2 dropdowns: ...

I am having trouble getting PHP to parse JSON in the way I need

Currently, I am working on a hobby project that involves an API for accessing data. While everything seems to be functioning well on the other ends, I am facing an issue with the JSON array format. I need the JSON array to not have brackets so that I can e ...

Unable to get jQuery click and hide functions to function properly

Hello, I am currently working on a program where clicking a specific div should hide its own class and display another one. However, the code does not seem to be functioning correctly. Below is my current implementation: $("#one").click(function(){ v ...

What is the best way to search for specific values within a complex nested JSON array in Postresql?

In my Posgresql database, I have a column containing json objects (jsonb type) structured like this: [ {"qos1": [ { "country_id" : [{"id":"IT",...}, {"id":"FR",...},...] },...],... ...

`the issue of $scope object not being passed correctly to ng-if and ng-class ternary conditions

**app.js:** $scope.servers = [ {name:'SQL Server', status:"up"}, {name:'Web Server', status:"down"}, {name:'Index Server', status:"down"} ]; **index.html:** <table> ...

The VueJS component failed to import successfully

Trying a simple demo to explore VueJS components, but encountering an error when loading the page: Unexpected Token Import in this line import GISView from './components/GISView.vue'; If I remove this line, GISView is not defined. Using Laravel ...

Issue with ng-selected not functioning properly

I am facing an issue where the ng-selected is assigned as true but the option is not being selected. The following is my controller code: .controller("PendingInvoiceCtrl", function($scope, $location, safeApply, dataService) { var userData = dataServi ...

Retrieve information in JSON structure

Currently, I am making a GET http request to the Google Civic's API. The purpose is to provide an address and in return receive information about members of the U.S. government based on the location of the provided address. // In the code below, the ...

JavaScript malfunctioning on Chrome

Displayed on the page is a table that lists user details. When the Edit Button is clicked, it allows for the row to be edited. The code provided below works in IE but does not show any response in Chrome. I am looking to make the site compatible with bot ...

Challenges with implementing a jQuery sortable table in Laravel

I've integrated tableclothjs and Twitter Bootstrap into my project, but the sortable headers are not appearing as expected. I've checked the JavaScript console for any errors or warnings, but there doesn't seem to be any issue indicated. Wh ...

What is the process for moving the final character to the beginning of a string?

Initially, the last letter in the string is being displayed. How can I rearrange it so that the last character appears first in the value? https://i.stack.imgur.com/uGq6H.jpg contentHtml += "<td rowspan1=\"" + 1 + "\" class=\"" + ( ...

Deactivate form fields when entering data into a set of interconnected fields

I need help with the following functionality using jQuery on the form below: 1/ If any character is entered in 'filter_loan_id', 'filter_fname', 'filter_lname', or 'filter_postcode' fields, then disable the 'fi ...

Is there a way to retrieve the Response object in Express from outside the Route

Currently, my backend API is built using Express JS. Instead of using the res object directly in the route controller, I am looking to access it from a custom service. While I know that I can simply pass res as an argument to the service function and use ...

Getting a value from a JavaScript variable and implementing it on a webpage (post) - A step-by-step guide

I am currently utilizing a third-party service that offers the ability to input custom HTML in order to gather information through a form. There is a form page where users input their details, and then there is a post form page. I reached out to the servic ...

Deciphering the $resource factory along with the @ symbol prefix

Under this particular service: vdgServices.factory('UserService', ['$resource', function($resource) { return $resource('api/users/:id', {}, { doGet: { method: 'GET', params: { i ...

Graphical user interface already constructed for importing .csv files and inserting information into a MySQL database

Looking for a plugin that can assist me in transferring a .csv file to a mySQL database. Initially, I was planning on creating this functionality myself but I believe that the necessary code may already exist. Below, you will find a detailed description of ...

Each loop in the forEach function triggers the mouseup event

I am currently utilizing a foreach loop: objects.forEach(function(object) { var button = '<tr><td>' + object.object.code + '</td><td>' + formatDistance(1456000) + &apos ...