Dynamically create object from object array

javascript add property to object dynamically
add property to array of objects typescript
typescript access object property dynamically
javascript array of objects
how to add dynamic value in array in javascript
create dynamic array with key and value in javascript
create object in jquery dynamically
javascript array push key value pair dynamically

I have an array of objects that I need to turn into a new single object.

This is structure of array:

class QueryFilter {

let filter1 = new QueryFilter();
filter1.filterName = "SpamScore";
filter1.filterValue = 5;

let filter2 = new QueryFilter();
filter2.filterName = "Pages";
filter2.filterValue = 50;

let filters = [filter1, filter2];

I need to turn filters into an object like this:

let newObj = {
    SpamScore: 5,
    Pages: 50

I have been trying with map and assign but I cant figure out the way I am supposed to do this. Does this require reflection of some type or is there a simple way?

You can do it using Array#reduce:

filters.reduce((r, i) => {
  r[i.filterName] = i.filterValue;
  return r;
}, {});

The idea is to loop over the filters object, and in each iteration, assign the key-value to the result.

how to dynamically create an array of objects?, Try this: for(var i=0; i<data.length; i++){ console.log(i); var obj = { "key1": data[i].​value1, "key2": data[i].value2}; tableData.push(obj); }. Array(height) creates an empty array of fixed size height - we want to create height rows..fill(null) is used to fill the array with any values, so you can map these values to new ones. Any other value can be used instead of null. Mapping empty array would return us new empty array.

A simple reduce into an object would do it:

class QueryFilter {

let filter1 = new QueryFilter();
filter1.filterName = "SpamScore";
filter1.filterValue = 5;

let filter2 = new QueryFilter();
filter2.filterName = "Pages";
filter2.filterValue = 50;

let filters = [filter1, filter2];

  filters.reduce((a, { filterName, filterValue }) => (
    Object.assign(a, { [filterName]: filterValue })
  ), {})

Dynamically creating properties on objects using javascript (Example), Defining a dynamic property like an Array on the Javascript Object. Let us take the same example as above: var obj = { property1: '', property2: '' };. Object and Dynamic Array in C#. In this article I explain how to create and use object and dynamic array in c#. An Array is a type that holds multiple variables of one type, allowing an index to access the individual values. It is possible to store an array holding multiple variables of multiple types using an object type array.

let newObj = {}
filters.forEach(x=> {
  newObj[x.filterName] = x.filterValue;


Dynamic objects in JavaScript, JavaScript has modernized object-oriented languages ​​with dynamic construction. It is possible to create an object by assigning a literal consisting in list of of the object, as with the elements of an array, provided they the object is part of  Object and Dynamic Array in C# An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location.

Hacks for Creating JavaScript Arrays, Arrays in JavaScript are high-level list-like objects with a length However, when you want to dynamically create arrays, the array literal syntax  You can use a dynamic object to refer to a dynamic script that is interpreted at run time. You reference a dynamic object by using late binding. In C#, you specify the type of a late-bound object as dynamic. In Visual Basic, you specify the type of a late-bound object as Object.

Dynamically append an element to a JavaScript Array in two ways , push() method, or you can leverage the array's “length” property to dynamically get the index of what would be the new element's position. I have provided  It can be useful when dealing with XML or JSON for quickly setting up a type to program against instead of always having to create data transfer objects. The ability for ExpandoObject to support data binding through INotifyPropertyChanged is a huge win for anyone using WPF, MVC, or any other binding framework in .NET as it allows you to use these “objects on the fly” as well as other statically typed classes.

How To Create, Modify, and Loop through Objects in JavaScript , JavaScript arrays are also a type of object. Objects are an integral and foundational aspect of most JavaScript programs. For example, a user  Currently, I am attempting to create a dynamically sized array of objects from a specific Blueprint class. From my previous experiences with Java, I figured I could create an array of references and set an index to a new object when needed, although I'm not entirely sure as to how I can approach this issue.

  • Can you post what you've tried?
  • I tried using assign and map and it didn't work so I don't see point of posting it as I am not sure if it is even those methods that I should be using.
  • for(let filter of filters) { newObj[filter.filterName] = filter.filterValue }
  • It's the same as object.property
  • more fully: let newObj = {}; for(let f of filters) { newObj[f.filterName] = f.filterValue }
  • Ahh thanks. Funnily that method didn't come up when i was googling, thank you very much i will accept when timer allows