es6-map

Javascript Map ordering

狂风中的少年 提交于 2019-12-11 16:05:34
问题 i recently had a bug in my code that was due to me missing the " in insertion order " text while looking through Map object details on MDN. In short, i have a map object, lets say let myMap = new Map; and then, after populating it, i iterate over its contents with a simple for .. of statement. Like this for (let [key, val] of myMap) { ... } The code in for loop depends on (key, value) pair to be sorted by key . However the algorithm that populates the map, does so in a random order(and i can

Typescript: How can I make entries in an ES6 Map based on a union type

半世苍凉 提交于 2019-12-11 05:26:57
问题 I would like to use Map instead of object map to declare some keys and values. But Typescript doesn't seem to support index types for ES6 Map, is that correct and are there any workarounds? Additionally, I would like to make the values type-safe as well so that each entry in the map has the correct type for the value corresponding to the key. Here is some pseudo-code that describes what I am trying to achieve: type Keys = 'key1' | 'key2'; type Values = { 'key1': string; 'key2': number; } /**

ES6 Map to return an array of object keys only

微笑、不失礼 提交于 2019-12-10 08:02:34
问题 I'm trying to write a method which will help me return an array of the object keys of all the currencies. But, I'm stuck at a point where I get the complete array of objects with key, value pair. And yes, I primarily need to use ES6 methods. I wouldn't want to use any other iterators. For e.g. : What I need: ['AED', 'ALL', 'AUD', 'EUR' .....] What I get : [{AED: {"isDefault": true}}, {ALL: {"isDefault": true}}, {AUD: {"isDefault": true}}, {'EUR': {"isDefault": true}}.....] Could you please

Using Array objects as key for ES6 Map

。_饼干妹妹 提交于 2019-12-10 01:52:16
问题 I am trying to update my code to ES6 as I am using Node 4.0 and really like its features so far. However I have problems with the new ES6 Map data structure as it behaves differently to {} when using Array as a key. I am using it as a counter map. I run this code and I would like to know how I can use arrays as keys for the Map . "use strict"; var a = new Map(); a.set(['x','y'], 1); console.log(a.get(['x','y'])); var b = {}; b[['x','y']] = 1; console.log(b[['x','y']]); It prints out the

Update the attribute value of an object using the map function in ES6

爷,独闯天下 提交于 2019-12-07 04:58:33
问题 I am trying to code this in ES6. Below is what I am trying to achieve. Let's say I have an array of objects called schools . let schools = [ {name: 'YorkTown', country: 'Spain'}, {name: 'Stanford', country: 'USA'}, {name: 'Gymnasium Achern', country: 'Germany'} ]; Now, I want to write a function called editSchoolName which will take 3 parameters, schools (which is the array I have defined above), oldName and name . I will pass the name of the school in the parameter oldName and that name

ES6 Map to return an array of object keys only

谁说我不能喝 提交于 2019-12-05 15:25:31
I'm trying to write a method which will help me return an array of the object keys of all the currencies. But, I'm stuck at a point where I get the complete array of objects with key, value pair. And yes, I primarily need to use ES6 methods. I wouldn't want to use any other iterators. For e.g. : What I need: ['AED', 'ALL', 'AUD', 'EUR' .....] What I get : [{AED: {"isDefault": true}}, {ALL: {"isDefault": true}}, {AUD: {"isDefault": true}}, {'EUR': {"isDefault": true}}.....] Could you please help me achieve this? Here's the code: var myJSON = { "countryCode": { "Australia": "AU", "United States"

Update the attribute value of an object using the map function in ES6

妖精的绣舞 提交于 2019-12-05 09:46:33
I am trying to code this in ES6. Below is what I am trying to achieve. Let's say I have an array of objects called schools . let schools = [ {name: 'YorkTown', country: 'Spain'}, {name: 'Stanford', country: 'USA'}, {name: 'Gymnasium Achern', country: 'Germany'} ]; Now, I want to write a function called editSchoolName which will take 3 parameters, schools (which is the array I have defined above), oldName and name . I will pass the name of the school in the parameter oldName and that name should be updated with the value in the parameter name . I don't want to change the state of the variable

Convert an array of objects with a unique id property to a Map

不想你离开。 提交于 2019-12-01 23:41:18
I have an array of objects, where each object has a unique member called id . How do I create a Map where the id if the Map's key? You want to reduce your array into a map: const arr = [{id:1},{id:2},{id:2}]; const map = arr.reduce((acc, item) => acc.set(item.id, item), new Map()); console.log(map.get(1)); Here is a JSPref against using map and forEach . In Chrome v53 reduce is fastest, then forEach with map being the slowest. You can use Array.prototype.map() to map the array elements to [element.id, element] pairs and then pass the resulting array to the Map constructor. const arr = [{id: 1,

How to iterate through JavaScript Maps using ng-repeat in angularjs?

浪子不回头ぞ 提交于 2019-12-01 08:28:35
I'm trying to use JavaScript Maps in ng-repeat angular's directive while searching I found that it could be done this way : <ul ng-controller="MyCtrl"> <li ng-repeat='(key, value) in {"First Name":"John", "Last Name":"Smith"}'> <span>Key: {{key}}, value: {{value}}</span> </li> </ul> but this works only with normal JSON objects, when I create a real Map the following way, it doesn't work. I have this controller for example function MyCtrl($scope) { $scope.items = new map(); $scope.items.set('adam',{'age':10,'fav':'doogie'}); $scope.items.set('amalie',{'age':12}); } and this html code <ul> <li

How to iterate through JavaScript Maps using ng-repeat in angularjs?

烈酒焚心 提交于 2019-12-01 06:49:03
问题 I'm trying to use JavaScript Maps in ng-repeat angular's directive while searching I found that it could be done this way : <ul ng-controller="MyCtrl"> <li ng-repeat='(key, value) in {"First Name":"John", "Last Name":"Smith"}'> <span>Key: {{key}}, value: {{value}}</span> </li> </ul> but this works only with normal JSON objects, when I create a real Map the following way, it doesn't work. I have this controller for example function MyCtrl($scope) { $scope.items = new map(); $scope.items.set(