How to Cast Object to List<Object> and Turn It into an Array

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



Leave a reply



Submit