This question already has answers here:
Convert form data to JavaScript object with jQuery
(58 answers)
Closed 2 years ago.
I want to form values to object/array like var date = {"first_name": "X", "last_name": "Y"};
I am using data = $(form).serialize() but it is produce first_name:X,last_name:Y
If I am using data = $(form).serializeArray() then it is taking other values too like select all options, so it is wrong.
So how to convert above key-value pairs to object using JavaScript?
serialize() is used to encode form data into URLs. Instead, use the FormData object.
const formdata = new FormData($("#formId"));
Related
This question already has answers here:
JSON encode MySQL results
(16 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 months ago.
I need to how I can use data that I fetched from the database and put into a JSON Object inside a php-file in another (Javascript)-File, where the data is supposed to end up inside an array.
function loadApplicationList()
{
$email = $_GET['email'];
$listcontent = queryDB("
SELECT Application.FavRank, Proposal.Title, Proposal.pdfName, RegisteredUser.Name
FROM Application
LEFT JOIN user_application
ON Application.ID = user_application.ID AND user_application.eMail = '$email'
LEFT JOIN Proposal
ON Proposal.ID = Application.Proposal_ID
LEFT JOIN RegisteredUser
ON RegisteredUser.eMail = Proposal.AuthorEmail;");
$encodeContent = json_encode($listcontent);
}
?>
Above is the php code, below the JS code
let data = [
// this array is supposed to contain the data from the JSON Object
];
I should also mention that I am new to these languages, so I might've already 'found' the right answer during my hour long research online but wasn't capable to identify it. Thanks for your help in advance.
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I am an array of hashes and I want to convert it to array of values something like this
array of hashes [{text: "James"},{text: "developer"}]
convert this to
array of values ["James", "Developer"]
Please help me achieve this in javascript.
Using Object.values, you can get the values of each object.
const input = [{text: "James"},{text: "developer"}];
const output = input.flatMap((item) => Object.values(item));
console.log(output);
This question already has answers here:
Javascript - convert an EXTRA LARGE Number to string in JSON before the default parsing
(4 answers)
Closed 4 years ago.
I've got an API which I make a get request to fetch data. When i try to save the Id, I see Javascript round the last digit of it and it makes my program to break!
I see THIS QUESTION but how can I save each Id as string?
I'm using a global array to store the selected items' data so, anyway to save one attribute of a JSON in string?
I'm going to have (for example) 3 items and make another get request for each Id:
axios.get(`http://api.nemov.org/api/v1/Market/Symbol/${this.props.ID}`)
One of those Ids, is: 9481703061634967 but JS convert that to 9481703061634968 so the get request is broken!
Any solution?
See my solution on this question:
Transform the response to string, then apply a repalce with a regex to
convert Id field to string type:
const axios = require("axios");
axios.get(url, {transformResponse: [data => data]}).then((response) => {
let parsed = JSON.parse(response.data.replace(/"Id":(\d+),/g, '"Id":"$1",'))
console.log(parsed)
});
Use this:
let strId = this.props.ID.toString();
axios.get(`http://api.nemov.org/api/v1/Market/Symbol/${strId}`)
This question already has answers here:
How do I write unencoded Json to my View using Razor?
(3 answers)
Closed 7 years ago.
I have ViewBag.Result with string value "[['Garreth','VP'],['Johan','IT'],['Test','QA']]"
I want to convert it as javascript array
var dataset =
[
['Garreth','VP'],
['Johan','IT'],
['Test','QA']
]
Obviously var dataset = '#ViewBag.Result' doesn't work because javascript treat is as string but not array. Any idea how to do this?
Thanks
Just remove the single quotes:
var dataset = #ViewBag.Result
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?
I am using the US Census API and end up with a two dimensional json array from a jQuery.get() request. My result (data) looks like this:
[["P0010001","NAME","state","county","tract"], ["2703","Census Tract 4001.01","17","119","400101"], ["5603","Census Tract 4001.02","17","119","400102"], ["4327","Census Tract 4002","17","119","400200"]]
It looks exactly like a two dimensional javascript array, but I cannot access it like one when I try:
var population = data;
alert(population[1][0]);
Is there a way to convert the json array into a javascript array, or to convert it to a string, which could then be put into an array?
Use JSON.parse:
var population = JSON.parse(data);
alert(population[1][0]);
JsFiddle: http://jsfiddle.net/6CGh8/