How to Convert a C# List<String[]> to a JavaScript Array

How do I convert a C# Liststring[] to a Javascript array?

You could directly inject the values into JavaScript:

//View.cshtml
<script type="text/javascript">
var arrayOfArrays = JSON.parse('@Html.Raw(Model.Addresses)');
</script>

See JSON.parse, Html.Raw

Alternatively you can get the values via Ajax:

public ActionResult GetValues()
{
// logic
// Edit you don't need to serialize it just return the object

return Json(new { Addresses: lAddressGeocodeModel });
}

<script type="text/javascript">
$(function() {
$.ajax({
type: 'POST',
url: '@Url.Action("GetValues")',
success: function(result) {
// do something with result
}
});
});
</script>

See jQuery.ajax

How to convert List in c# to array in javascript and check the array item

You can convert the C# List into a Javascript array by using

<script>
var javascriptArray = @Html.Raw(Json.Encode(Model.ListToConvertToJavascript));
</script>

This will create a Json object with the data inside the List in your Model and you will be able to iterate through it in plain Javascript.

The syntax error might you get is the IDE telling you that you are using a syntax that the Javascript parser doesn't understand, which is the RAZR sentece @Html(...); you can ignore it.

Test it out using:

for(int i = 0; i < $(javascriptArray).lenght; i++){
alert(javascriptArray[i]);
}

or opening your browser developer's console and typing the name of the array you are creating to see if it is populated by your Model List data.

Convert C# List of object to JavaScript array of objects

Comments by Jason P and Rob G are correct. The two formats in questions are equivalent. Turns out my issue was with the casing. DOH!

I just changed the properties in my class to be lower case. Thought, I would love to see an alternate solution. I will choose this one until a better one is submitted.

pass list item from c# to javascript array

Using Json.Net

var obj = new[] { 
new object[] { "Bondi Beach", -33.890542, 151.274856, 4 },
new object[] { "Coogee Beach", -33.923036, 151.259052, 5 },
new object[] { "Cronulla Beach", -34.028249, 151.157507, 3 },
new object[] { "Manly Beach", -33.80010128657071, 151.28747820854187, 2 },
new object[] { "Maroubra Beach", -33.950198, 151.259302, 1 },
};
var json = JsonConvert.SerializeObject(obj);

Convert array of strings to Liststring

Just use this constructor of List<T>. It accepts any IEnumerable<T> as an argument.

string[] arr = ...
List<string> list = new List<string>(arr);

Liststring to object[]

You can do it using ToArray generic method:

var list = new List<string>();
object[] array = list.ToArray<object>();

You don't even need Cast<object>, because ToArray takes IEnumerable<T> which is covariant on generic type parameter.

Converting C# Liststring To Javascript

To get rid of the 'syntax error' you just need to remove ; at the end

var imageLinks = @Html.Raw(Json.Encode(Model.ImgLinks))

Despite the warning your code will run fine anyway.

Here's a different kind of solution to the problem if anyone's interested. You can loop through the razor collection and store the values in a Javascript Array like this.

<script type="text/javascript">

var myArray = [];

@foreach (var link in Model.ImgLinks)
{
@:myArray.push("@link");
}

</script>

Splitting a string into a List of string array in C#

If you really want a List of string-Arrays, just omit the "ToDictionary" line.

var result = str.Split(',')
.Select(line => line.Split('='))
.ToList();

will give you a List of Arrays, with each Array [0] holding the "Key", and [1] holding the "Value"

ad PS: To get the first "OU" value, you can use a regular expression:

var firstOuRegex = new Regex("[oO][uU]=([^,]+)", RegexOptions.Compiled);
var testString = "CN=Test,OU=ABC,OU=Company,DC=CFLA,DC=domain";

var result = firstOuRegex.Match(testString).Groups[1];

Convert Liststring into Liststring[] with certain array size

Isn't it a List<string> to string[]?

string[] ArrayOfStrings = MyList.ToArray()

If you want parts with sizes, you can do it:

 int size = 5;
List<string[]> ArrList = new List<string[]>();
for (var i = 0; i < myList.Count; i+=size)
{
ArrList.Add(myList.Skip(i).Take(size).ToArray());
}

I think that should do it.



Related Topics



Leave a reply



Submit