JavaScript Equivalent of C# ToDictionary

javascript transform object
javascript object reverse lookup
javascript object from
array to object javascript
convert array of objects to object javascript
convert map to object javascript
es6 object from array
object fromentries is not a function

I am looking for a way to take the following list:

directory = [
    {
        name: "Albert",
        age: 40,
        gender: "M"
    },
    {
        name: "Suzanne",
        age: 27,
        gender: "F"
    },
    {
        name: "Robert",
        age: 19,
        gender: "M"
    },
    {
        name: "Connie",
        age: 87,
        gender: "F"
    }
]

and make a dictionary on the key name:

dictionary = {
    "Albert": {
        name: "Albert",
        age: 40,
        gender: "M"
    },
    "Suzanne": {
        name: "Suzanne",
        age: 27,
        gender: "F"
    },
    "Robert": {
        name: "Robert",
        age: 19,
        gender: "M"
    },
    "Connie": {
        name: "Connie",
        age: 87,
        gender: "F"
    }
}

This is similar to the C# ToDictionary method. I know I could do something like iterate over directory in a for loop or .each call, and modify the value of dictionary each iteration. I would prefer, however, to make a functional programming-like assignment instead, e.g.

dictionary = directory.toDictionary(p => p.name);

Does such a method exist, say within ES6 or lodash?

You could map key and value and create an object from this arrays with (upcoming) Object.fromEntries.

var directory = [{ name: "Albert", age: 40, gender: "M" }, { name: "Suzanne", age: 27, gender: "F" }, { name: "Robert", age: 19, gender: "M" }, { name: "Connie", age: 87, gender: "F" }],
    result = Object.fromEntries(directory.map(o => [o.name, o]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

A javascript equivalent for toDictionary (c#), When providing an object, I don't think it makes sense to pass the index into the callbacks. Rather, pass in the current key. Also, I think reduce� A javascript equivalent for toDictionary (c#) Ask Question Asked 4 years, 1 month ago. but a data source that implements IEnumerable in C# is a collection - not

You could reduce the array. Use Object.assign() to add each name as a property

const directory=[{name:"Albert",age:40,gender:"M"},{name:"Suzanne",age:27,gender:"F"},{name:"Robert",age:19,gender:"M"},{name:"Connie",age:87,gender:"F"}],
    output = directory.reduce((r, o) => Object.assign(r, { [o.name]: o }), {})

console.log(output)

JavaScript ToDictionary, Anyone familiar with Linq's ToDictionary method may be looking for a The cool thing about JavaScript is that first-order properties var data = [ { id: 1, name: ' Brian'}, { id: 2, name: 'Jake'}, { id: 3, name: 'Tim'} ]; // Convert to� The Array functions in Javascript are similar in result, but not in implementation with C# LINQ. If you have an array with a million rows and you filter it, then map it, then get the first item, you get an array at each step: a million filterings and a thousand item array, a thousand mappings in another thousand item array. all for one item.

Javascript's Map closely resembles a dictionary. Its instances sport handy has, get, set, keys, values methods. It's also directly iterable via a forEach method.

You can construct a Map using an array of "key-value-pairs". (in quotes, because in reality we're using arrays in some tuple-like fashion).

To create a Map to use as a dictionary, you'd do:

const directory=[{name:"Albert",age:40,gender:"M"},{name:"Suzanne",age:27,gender:"F"},{name:"Robert",age:19,gender:"M"},{name:"Connie",age:87,gender:"F"}];

const myDict = new Map(
  directory.map(p => [p.name, p])
);

console.log("Has Albert:", myDict.has("Albert"))

myDict.forEach(p => { console.log(`${p.name} has age ${p.age}`) });

JavaScript equivalents of some common C# LINQ methods. To help , JS-LINQ.js. // JS array equivalents to C# LINQ methods - by Dan B. reduce is " kinda" equivalent to Aggregate (and also can be used to Sum). var aggregate� TypeScript has no equivalent for the language-integrated-natural-query aspect of LINQ. The C# version throws an exception // if Wild Web is a scary and

Since you asked by lodash solution too, you can use the .keyBy() method of that library.

const directory = [{name:"Albert",age:40,gender:"M"},{name:"Suzanne",age:27,gender:"F"},{name:"Robert",age:19,gender:"M"},{name:"Connie",age:87,gender:"F"}];

console.log(_.keyBy(directory, o => o.name));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.14/lodash.min.js"></script>

JavaScript Basics: How to create a Dictionary with Key/Value pairs , This flexibility allows for the “Object” type to be used in ways that might seem strange when compared to statically typed languages such as C#. The JavaScript “Object” type is very versatile since JavaScript is a dynamically typed language. This flexibility allows for the “Object” type to be used in ways that might seem strange when compared to statically typed languages such as C#. Creating a Dictionary in JavaScript

There's no specific built-in but it's easy with .reduce():

let dict = directory.reduce((d, v) => (d[v.name] = v, d), {});

As pointed out in a good comment on the question, one thing you need to consider is the meaning of duplicate names. Is that a "bad data" situation? Should there be an array of names for such duplicates? Questions like that are specific the the data structure and its purpose in your specific application.

LINQ ToDictionary() Method, Following is the syntax of using LINQ ToDictionary operator to convert collection to new dictionary object. C# Code. var student = objStudent.ToDictionary(x => x. I have this Dictionary in C#. Dictionary<string, string [] > I serialized the string and now I want to get all the keys in javascript and show an alert message with the key and also load value into a var StringArrayItems I have this in jscript var dictionary = eval( "(" + dict + ")" ); And now I want something like this

linq.js - LINQ for JavaScript, ToDictionary, ToInfinity,ToJSON, ToLookup, ToNegativeInfinity, ToObject, ToString, Trace, Unfold, Union, Where, Write, WriteLine, Zip. see details - linq.js� Building up dictionaries can require a significant amount of code. When we use ToDictionary, we can use less code to create a Dictionary. Important: The 2 arguments to ToDictionary are lambdas: the first sets each key, and the second sets each value. Lambda: Look at the "v => v" style lines. Lambda can be expressed as "goes to," meaning each

add new dictionary to dictionary js Code Example, Get code examples like "add new dictionary to dictionary js" instantly right a Datagridview in windows form using C# � bindbidirectional vue js� Despite this, I am not such productive with C#, as I am with JavaScript, and I often find myself asking colleagues, or searching the Internet for “equivalent of JavaScript [feature] in C#“… I rarely get disappointed. In this post we explore the equivalent in C# of some of the most useful JavaScript array methods. Language INtegrated Query

C# Dictionary to Javascript, Why not use AJAX (XMLHttpRequest) and get the string formed directly from server in your Javascript? I am not too sure that we can traverse� In C#, Dictionary is a generic collection which is generally used to store key/value pairs. The working of Dictionary is quite similar to the non-generic hashtable.The advantage of Dictionary is, it is generic type.

Comments
  • You can do it with .reduce().
  • Note the first structure allows more than one entry for the same name, while the one you want don't.
  • linq.js offers a toDictionary method.
  • You can write it. Think: extension methods; just a warning, in JS they are global, so be careful about collisions with other libraries/ES6/7/8/9... But ultimately, a good old for(let i=0; i<array.length; ++i) loop is still the fastest way to iterate an Array.
  • you should be using Map - it was specifically designed for creating a lookup table like this
  • This is a good solution, since it supports elderly browsers and Js standards. Object.fromEntries will require ES2017 lib to be imported, if you use tsconfig and Angular for example.