How to cast object to List<object> and turn it into an array?
As discussed in the comments, what you're actually after is to take a List<object>
(which each object
is actually a T
, and might be a boxed value type), and turn it into either a List<T>
or a T[]
, depending on a bool flag.
The easiest thing to do is to modify your CastList method slightly:
var castMethod = this.GetType().GetMethod("CastList").MakeGenericMethod(type);
value = castMethod.Invoke(null, new object[] { value, isArray });
public static object CastList<T>(List<object> o, bool isArray) {
var result = o.Cast<T>();
return isArray ? (object)result.ToArray() : result.ToList();
}
How to convert an Object {} to an Array [] of key-value pairs in JavaScript
You can use Object.keys()
and map()
to do this
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map((key) => [Number(key), obj[key]]);
console.log(result);
How to convert object containing Objects into array of objects
This works for me
var newArrayDataOfOjbect = Object.values(data)
In additional if you have key - value object try:
const objOfObjs = {
"one": {"id": 3},
"two": {"id": 4},
};
const arrayOfObj = Object.entries(objOfObjs).map((e) => ( { [e[0]]: e[1] } ));
will return:
[
{
"one": {
"id": 3
}
},
{
"two": {
"id": 4
}
}
]
convert list of object to array
a field initializer cannot reference the nonstatic field method or
property
The issue is that you have a field initializer referencing a non-static field, method or property. The C# compiler won't allow that.
One solution is to move from a field
to a property
:
Gene[] s { get { return QestionList.ToArray(); } }
The downside of above is that whenever you access s
you are effectively cloning QestionList
(i.e. it is expensive).
Another would be to leave your field there:
Gene[] s;
and populate it inside your constructor instead:
s = QestionList.ToArray();
The upside of doing it in the constructor is that the cloning will occur only once. That is also the downside (if you are altering QestionList
then s
won't reflect that).
Convert an ArrayList to an object array
Something like the standard Collection.toArray(T[]) should do what you need (note that ArrayList
implements Collection
):
TypeA[] array = a.toArray(new TypeA[a.size()]);
On a side note, you should consider defining a
to be of type List<TypeA>
rather than ArrayList<TypeA>
, this avoid some implementation specific definition that may not really be applicable for your application.
Also, please see this question about the use of a.size()
instead of 0
as the size of the array passed to a.toArray(TypeA[])
Convert List<Object[]> to list<String> Cast exception
- Stream the arrays
- flatten the arrays into a stream of objects.
- filter out non strings.
- cast via a map remaining objects to strings
- collect into a List
List<Object> params = List.of(1, 2,
"hello",
"computer", new Object(), 4.32,
new Object[]{1,2});
List<String> strings = params.stream()
.filter(ob -> ob instanceof String)
.map(ob -> (String) ob)
.collect(Collectors.toList(), "java");
System.out.println(strings);
Prints
[hello, computer, java]
Converting Object to Array using ES6 features
Use (ES5) Array::map
over the keys
with an arrow function (for short syntax only, not functionality):
let arr = Object.keys(obj).map((k) => obj[k])
True ES6 style would be to write a generator, and convert that iterable into an array:
function* values(obj) {
for (let prop of Object.keys(obj)) // own properties, you might use
// for (let prop in obj)
yield obj[prop];
}
let arr = Array.from(values(obj));
Regrettably, no object iterator has made it into the ES6 natives.
Related Topics
C#, Access Child Properties from Parent Reference
How to Get Nested Json Values in C#
Controlling Datetime Parameter Formatting in Webapi 2
Sorting String Values Without Using Any Method/Function
Using Formflow Dialog in Bot Framework
How to Get the Xml Soap Request of an Wcf Web Service Request
Data Binding in MVC 5 and Select2 Multiple Values With Razor Engine
Encode a Filestream to Base64 With C#
How to Ignore Convert to Nullable in Lambda Expressions
Load Image from Resources Area of Project in C#
How to Set a Cookie on Httpclient'S Httprequestmessage
How to Submit Disabled Input in ASP.NET MVC
How to Get the Current Project Directory from C# Code When Creating a Custom Msbuild Task
Validate Indian Phone Number With Optional +91 or 0 Preceeding 10 Digits
Getting Value of Selected Item in List Box as String
How to Set Value of Wpf Combobox Item from C# Code
Download File from an ASP.NET Web API Method Using Angularjs