-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjEntries.js
More file actions
190 lines (161 loc) · 4.19 KB
/
objEntries.js
File metadata and controls
190 lines (161 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
console.log('----entries----')
const obj = {
array: [1, 3, 5],
string: 'hello',
arrayInArray: [1, [3, 4, [3]]],
object: {
key: 'value',
},
bool: true,
}
const entrie = Object.entries(obj)
console.log(entrie)
// order is not guaranteed
for ([key, value] of entrie) {
console.log(key, value)
}
console.log('------')
// order is guaranteed
for (key in obj) {
console.log(key, obj[key])
}
// ----- object formEntries
console.log('\n----fromEntries----')
// transforms a list of key-value pairs into an object
console.log(entrie)
const cloneUsingFromEntries = Object.fromEntries(entrie)
console.log(cloneUsingFromEntries)
console.log('\n-----exercise-----')
/**
* ## Title
*
* ### Instruction
*
* Write the following set functions :
*
* - `filterKeys` has as parameters the object and the function for the filtering
* - `filterValues` the same here
*
* Does function acts like .filter from array
*
* - `mapKeys` has as parameters the object and the function for the filtering
* - `mapValues` the same
* - `mapEntries`
*
* Does function acts like .map from array
*
* - `reduceKeys`
* - `reduceValues`
* - `reduceEntries`
*
* Does function acts like .reduce from array
*
*
*/
// /* */ //
// filter
const filterObj = (obj, filtering, keyVal) => {
let entrie = Object.entries(obj).filter(
(ele) => filtering(ele[keyVal]) && ele
)
let result = Object.fromEntries(entrie)
return result
}
const filterKeys = (obj, filtering) => filterObj(obj, filtering, 0)
const filterValues = (obj, filtering) => filterObj(obj, filtering, 1)
// map
const mapObj = (obj, f, keyVal) => {
let entrie = Object.entries(obj).map((ele) => {
if (!keyVal) {
ele = f(ele)
return ele
}
ele[keyVal - 1] = f(ele[keyVal - 1])
return ele
})
let result = Object.fromEntries(entrie)
return result
}
const mapKeys = (obj, f) => mapObj(obj, f, 1)
const mapValues = (obj, f) => mapObj(obj, f, 2)
const mapEntries = (obj, f) => mapObj(obj, f)
// reduce
const reduceKey = (obj, f, init) => {
return Object.keys(obj).reduce(f, init)
}
const reduceValues = (obj, f, init) => {
return Object.values(obj).reduce(f, init)
}
const reduceEntries = (obj, f, init) => {}
// /* */ //
// filter keys
console.log(filterKeys(obj, (k) => k == 'object'))
console.log(filterKeys(obj, (k) => typeof k == 'string'))
console.log(filterKeys(obj, (k) => /a/.test(k)))
console.log(filterKeys(obj, (k) => k.length <= 4))
console.log(filterKeys(obj, (k) => k == 'country'))
console.log('---filter value----')
// filter values
console.log(filterValues(obj, (v) => typeof v == 'string'))
console.log(filterValues(obj, (v) => Array.isArray(v)))
console.log(filterValues(obj, (v) => typeof v == 'object'))
console.log(
filterValues(obj, (v) => v && (v.constructor === Object || !v.constructor))
)
console.log(filterValues(obj, (v) => /a/.test(v)))
console.log('---map key----')
// map key
console.log(mapKeys(obj, (k) => `obj_${k}`))
console.log('---map values----')
// map key
// console.log(
// mapValues(object, (ele) =>
// typeof ele === 'string' ? `category_2_${ele}` : ele
// )
// )
// console.log(
// mapValues(object, (ele) =>
// typeof ele === 'function' ? () => `record deleted!!` : ele
// )
// )
// console.log(mapValues(object, (ele) => (!ele ? true : ele)))
// console.log(
// mapValues(object, (ele) =>
// ele && (ele.constructor === Object || !ele.constructor)
// ? mapValues(ele, (v) => `ESP_"${v}"`)
// : ele
// )
// )
// console.log(mapValues(object, (ele) => (Array.isArray(ele) ? ele.sort() : ele)))
console.log(
mapValues(obj, (v) =>
Array.isArray(v)
? v.flat(Infinity)
: typeof v == 'string'
? v.split('')
: typeof v == 'boolean'
? false
: mapKeys(v, (k) => `depth_${k}`)
)
)
console.log('---map entries----')
// map entries
console.log(
mapEntries(obj, ([k, v]) =>
typeof v == 'string' ? [`obj_${k}`, `value_${v}`] : [`obj_${k}`, v]
)
)
console.log('---reduce key----')
// reduce entries
console.log(reduceKey(obj, (acc, cr) => acc + ' ' + cr, ''))
console.log(reduceKey(obj, (acc, cr) => acc + ' ' + cr))
console.log(
reduceValues(
obj,
(acc, cr) => {
acc.push(typeof cr)
return acc
},
[]
)
)