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
Related
This question already has answers here:
Flattening multidimensional arrays in javascript
(2 answers)
Merge/flatten an array of arrays
(84 answers)
Closed 20 days ago.
I want to get a String value of multi-dimensional array
and push it into a new Array (That'd be an answer!)
For Example :
var dimStr = [ "First-one", ["Double-one", "Double-two", "Double-three",
["Triple-one", "Triple-two"] ], "First-two" ] ;
And the output should be :
output = ["First-one", "Double-one", "Double-two", "Double-three", "Triple-one",
"Triple-two", "First-two" ] ;
Thank you very much in advance for your kindness.
The output should be a new Array that contains every String value from dimStr
Look at the array method .flat().
output = dimStr.flat(3);
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"));
This question already has answers here:
How can I convert a comma-separated string to an array?
(19 answers)
Closed 3 years ago.
*****No JQUERY*****
I have a string passed into my Javascript that looks like below. I want to convert it into an array.
I have
{"test":"1,180,35"}
I want
an array where index 0 = 1, index 1 = 180, index 2 = 35.
How would I achieve this?
Parse the string, pull out the property value for property test, split it on ,.
var input = '{"test":"1,180,35"}'
var jsObj = JSON.parse(input);
var arr = jsObj.test.split(",");
console.log(arr);
use JSON.parse() to convert a string into a json object.
But, you are looking to parse a series of numbers into an array, so what you really want is split(",")
Use the JSON object
let arr = JSON.parse('{"test":"1,180,35"}').test.split(',');
For example:
var yourData = `{"test":"1,180,35"}`
JSON.parse(yourData).split(',')
This question already has answers here:
Convert string array representation back to an array
(3 answers)
Closed 5 years ago.
I have a string of integers
str = "[7,2,7,7,2,7,7,4,3,2]"
and i want to get an array so that i can manipulate the data easily, but i have no idea how to do it. Can you help me ? I'm sure it's a basic task but i am not very familiar with node.
Thank you.
Try JSON.parse(yourStr). Alternately:
yourString.substr(1,arr.length-2).split(",").map((el) => {
return parseInt(el)
})
Looks like JSON, parse it with JSON.parse
numbers = JSON.parse("[7,2,7,7,2,7,7,4,3,2]")
This question already has answers here:
How to parse CSV data?
(14 answers)
Closed 8 years ago.
How can I open a .csv file and turn values into a javascript array.
In classical programming i'd do this by:
opening the file as a string
splitting by ,
close the file
I know how to split, .split(','), but how do I open and close the csv file in javascript or jquery?
$.get(url,function(data)
{
var MainArray = data.split('\n');
for(var i=0;i<MainArray.length;i++)
{
MainArray[i] = MainArray[i].split(',');
// now MainArray is a two dimensional array that contains csv file
//MainArray[row index][column index]
}
}
);