10 examples of 'js convert object to map' in JavaScript

Every line of 'js convert object to map' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
22static toMap(obj: object): Map {
23 return new Map(Object.entries(obj).map(pair => [pair[0], new CastedObject(pair[1])]));
24}
40function toObject(map) {
41 const object = {};
42 for (const attr in map) {
43 let value = map[attr];
44 if (value === 'true') {
45 value = true;
46 }
47 else if (value === 'false') {
48 value = false;
49 }
50 else if (value === 'null') {
51 value = null;
52 }
53 else if (String(Number(value)) === value) {
54 value = Number(value);
55 }
56 object[attr] = value;
57 }
58 return object;
59}
12function toObjMap(obj) {
13 /* eslint-enable no-redeclare */
14 if (Object.getPrototypeOf(obj) === null) {
15 return obj;
16 }
17
18 var map = Object.create(null);
19
20 for (var _i2 = 0, _objectEntries2 = (0, _objectEntries3.default)(obj); _i2 < _objectEntries2.length; _i2++) {
21 var _ref2 = _objectEntries2[_i2];
22 var key = _ref2[0];
23 var value = _ref2[1];
24 map[key] = value;
25 }
26
27 return map;
28}
21export function objToMap(obj) {
22 return Object.keys(obj || {}).reduce((out, key) => out.set(key, obj[key]), new Map());
23}
2export default function toObjMap(obj) {
3 /* eslint-enable no-redeclare */
4 if (Object.getPrototypeOf(obj) === null) {
5 return obj;
6 }
7
8 var map = Object.create(null);
9
10 for (var _i2 = 0, _objectEntries2 = objectEntries(obj); _i2 < _objectEntries2.length; _i2++) {
11 var _ref2 = _objectEntries2[_i2];
12 var key = _ref2[0];
13 var value = _ref2[1];
14 map[key] = value;
15 }
16
17 return map;
18}
143function convertObject(obj: {[k: string]: unknown}) {
144 for (const key of Object.keys(obj)) {
145 obj[key] = convertValue(obj[key]);
146 }
147 return obj;
148}
1634function mapObject(obj, callback) {
1635 var result = {};
1636 Object.keys(obj).forEach(function (key) {
1637 var value = callback(key, obj[key]);
1638 if (value !== undefined) {
1639 result[key] = value;
1640 }
1641 });
1642 return result;
1643}
60export function wrapToMap(obj) {
61 if (!obj) {
62 return {};
63 }
64 return obj;
65}
18export function convertObjectToArray(object: {[key: string]: string}): LogItem[] {
19 const ret = [];
20
21 Object.keys(object).forEach((key) => {
22 if (object.hasOwnProperty(key)) {
23 const value = object[key];
24 ret.push({key, value});
25 }
26 });
27
28 return ret;
29}
16export default function mapObject(obj: Record, iteratee: Iteratee) {
17 const keys = Object.keys(obj);
18 const mappedObject = {};
19
20 keys.forEach(key => {
21 mappedObject[key] = iteratee(obj[key], key);
22 });
23
24 return mappedObject;
25}

Related snippets