-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.html
385 lines (313 loc) · 15.5 KB
/
Object.html
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.js"></script>
<script>
/*_.assign(object, [sources])
分配来源对象的可枚举属性到目标对象上。 来源对象的应用规则是从左到右,随后的下一个对象的属性会覆盖上一个对象的属性。 */
function Foo() {
this.c = 3;
}
function Bar() {
this.e = 5;
}
Foo.prototype.d = 4;
Bar.prototype.f = 6;
console.log(_.assign({ 'a': 1 }, new Foo, new Bar));
// => { 'a': 1, 'c': 3, 'e': 5 }
/*.assignIn(object, [sources])这个方法类似 _.assign。 除了它会遍历并继承来源对象的属性。*/
function Foo() {
this.b = 2;
}
function Bar() {
this.d = 4;
}
Foo.prototype.c = 3;
Bar.prototype.e = 5;
console.log(_.assignIn({ 'a': 1 }, new Foo, new Bar));
// => { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 }
/*_.assignInWith(object, sources, [customizer])这个方法类似 _.assignIn。 除了它接受一个 customizer决定如何分配值。 customizer` 会传入5个参数:(objValue, srcValue, key, object, source)。 */
function customizer(objValue, srcValue) {
/* console.log(_.isUndefined(objValue));
console.log(objValue);*/
return _.isUndefined(objValue) ? srcValue : objValue;
/*重定义会覆盖之前的值*/
/*obj:{b:2,a:1} src:{a:3}*/
}
var defaults = _.partialRight(_.assignInWith, customizer);
console.log(defaults({ 'a': 1 }, { 'b': 2 },{ 'a': 3 }));
// => { 'a': 1, 'b': 2 }
/*_.assignWith(object, sources, [customizer])*/
function customizer(objValue, srcValue) {
return _.isUndefined(objValue) ? srcValue : objValue;
}
var defaults = _.partialRight(_.assignWith, customizer);
defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
// => { 'a': 1, 'b': 2 }
/*_.at(object, [paths]) 根据 object 的路径获取值为数组。*/
var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
_.at(object, ['a[0].b.c', 'a[1]']);// => [3, 4]
_.at(['a', 'b', 'c'], 0, 2);// => ['a', 'c']
/*_.create(prototype, [properties])创建一个继承 prototype 的对象。 如果提供了 properties,它的可枚举属性会被分配到创建的对象上。*/
function Shape() {
this.x = 0;
this.y = 0;
}
function Circle() {
Shape.call(this);
}
Circle.prototype = _.create(Shape.prototype, {
'constructor': Circle
});
var circle = new Circle;
console.log(circle instanceof Circle);// => true
console.log(circle instanceof Shape);// => true
/*_.defaults(object, [sources])分配来源对象的可枚举属性到目标对象所有解析为 undefined 的属性上。 来源对象从左到右应用。一旦设置了相同属性的值,后续的将被忽略掉。*/
_.defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
// => { 'user': 'barney', 'age': 36 }
/*_.defaultsDeep(object, [sources])这个方法类似 _.defaults,除了它会递归分配默认属性。*/
_.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } });
// => { 'user': { 'name': 'barney', 'age': 36 } }
/*_.findKey(object, [predicate=_.identity])这个方法类似 _.find。除了它返回最先被 predicate 判断为真值的元素 key,而不是元素本身。*/
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });// => 'barney'(无法保证遍历的顺序)
// 使用了 `_.matches` 的回调结果
_.findKey(users, { 'age': 1, 'active': true });// => 'pebbles'
// 使用了 `_.matchesProperty` 的回调结果
_.findKey(users, ['active', false]);// => 'fred'
// 使用了 `_.property` 的回调结果
_.findKey(users, 'active');// => 'barney'
/*_.findLastKey(object, [predicate=_.identity])这个方法类似 _.findKey。 不过它是反方向开始遍历的。*/
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findLastKey(users, function(o) { return o.age < 40; });
// => 返回 'pebbles', `_.findKey` 会返回 'barney'
// 使用了 `_.matches` 的回调结果
_.findLastKey(users, { 'age': 36, 'active': true });// => 'barney'
// 使用了 `_.matchesProperty` 的回调结果
_.findLastKey(users, ['active', false]);// => 'fred'
// 使用了 `_.property` 的回调结果
_.findLastKey(users, 'active');// => 'pebbles'
/*_.forIn(object, [iteratee=_.identity])使用 iteratee 遍历对象的自身和继承的可枚举属性。 iteratee 会传入3个参数:(value, key, object)。 如果返回 false,iteratee 会提前退出遍历。*/
function Boo() {
this.a = 1;
this.b = 2;
}
Boo.prototype.c = 3;
_.forIn(new Boo, function(value, key) {
console.log(key);
});
// => 输出 'a', 'b', 然后 'c' (无法保证遍历的顺序)
/*_.forInRight(object, [iteratee=_.identity])这个方法类似 _.forIn。除了它是反方向开始遍历的。*/
function Aoo() {
this.a = 1;
this.b = 2;
}
Aoo.prototype.c = 3;
_.forInRight(new Aoo, function(value, key) {
console.log(key);
});
// => 输出 'c', 'b', 然后 'a', `_.forIn` 会输出 'a', 'b', 然后 'c'
/*_.forOwn(object, [iteratee=_.identity])使用 iteratee 遍历自身的可枚举属性。 iteratee 会传入3个参数:(value, key, object)。 如果返回 false,iteratee 会提前退出遍历。*/
function Coo() {
this.a = 1;
this.b = 2;
}
Coo.prototype.c = 3;
_.forOwn(new Coo, function(value, key) {
console.log(key);
});
// => 输出 'a' 然后 'b' (无法保证遍历的顺序
/*_.forOwnRight(object, [iteratee=_.identity])这个方法类似 _.forOwn。除了它是反方向开始遍历的。*/
/*_.functions(object)返回一个 function 对象自身可枚举属性名的数组。*/
function Foo() {
this.a = _.constant('a');
this.b = _.constant('b');
}
Foo.prototype.c = _.constant('c');
_.functions(new Foo);// => ['a', 'b']
/*_.functionsIn(object)返回一个 function 对象自身和继承的可枚举属性名的数组。*/
function Foo() {
this.a = _.constant('a');
this.b = _.constant('b');
}
Foo.prototype.c = _.constant('c');
_.functionsIn(new Foo);// => ['a', 'b', 'c']
/*_.get(object, path, [defaultValue])根据对象路径获取值。 如果解析 value 是 undefined 会以 defaultValue 取代。*/
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');// => 3
_.get(object, ['a', '0', 'b', 'c']);// => 3
console.log( _.get(object, 'a.b.c', 'default'));// => 'default'
/*_.has(object, path)检查 path 是否是对象的直接属性*/
var object = { 'a': { 'b': { 'c': 3 } } };
console.log(object);
var other = _.create({ 'a':_.create({ 'b': _.create({ 'c': 3 }) }) });
console.log(other);
_.has(object, 'a');// => true
console.log( _.has(object, 'b'));
_.has(object, 'a.b.c');// => true
_.has(object, ['a', 'b', 'c']);// => true
_.has(other, 'a');// => false
console.log( _.has(other, 'b'));// => false create创建的是继承的对象
/*_.hasIn(object, path) 检查 path 是否是object对象的直接或继承属性。*/
var object = _.create({ 'a': _.create({ 'b': 2 }) });
_.hasIn(object, 'a');// => true
_.hasIn(object, 'a.b');// => true
_.hasIn(object, ['a', 'b']);// => true
_.hasIn(object, 'b');// => false /*路径不对 b是a的继承属性*/
/*_.invert(object) 创建一个object键值倒置后的对象。 如果 object 有重复的值,后面的值会覆盖前面的值。*/
var object = { 'a': 1, 'b': 2, 'c': 1 };
console.log(_.invert(object));
// => { '1': 'c', '2': 'b' }
/*_.invertBy(object, [iteratee=_.identity]) 倒置对象 是 collection(集合)中的每个元素经过 iteratee(迭代函数) 处理后返回的结果。每个反转键相应反转的值是一个负责生成反转值key的数组。*/
var object = { 'a': 1, 'b': 2, 'c': 1 };
console.log(_.invertBy(object));
// => { '1': ['a', 'c'], '2': ['b'] }
console.log(_.invertBy(object, function(value) {
return 'group' + value;
}));
// => { 'group1': ['a', 'c'], 'group2': ['b'] }
/*_.invoke(object, path, [args]) 调用object对象path上的方法。*/
var object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] };
console.log(_.invoke(object, 'a[0].b.c.slice', 1, 3));// => [2, 3]
/*_.keys(object) 创建一个 object 的自身可枚举属性名为数组。非对象的值会被强制转换为对象*/
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
console.log(_.keys(new Foo));// => ['a', 'b'] (iteration order is not guaranteed)
console.log(_.keys('hi'));// => ['0', '1']
/*_.keysIn(object) 创建一个 object 自身 和 继承的可枚举属性名为数组.非对象的值会被强制转换为对象。 */
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
console.log(_.keysIn(new Foo));
// => ['a', 'b', 'c'] (iteration order is not guaranteed)
/*_.mapKeys(object, [iteratee=_.identity]) 这个方法创建一个对象,对象的值与object相同,并且 key 是通过 iteratee 运行 object 中每个自身可枚举属性名字符串 产生的*/
console.log(_.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
return key + value;
}));
// => { 'a1': 1, 'b2': 2 }
/*_.mapValues(object, [iteratee=_.identity])创建一个对象,这个对象的key与object对象相同,值是通过 iteratee 运行 object 中每个自身可枚举属性名字符串产生的。 */
var users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
};
_.mapValues(users, function(o) { return o.age; });
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
// The `_.property` iteratee shorthand.
_.mapValues(users, 'age');
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
/*_.merge(object, [sources]) 类似_.assign, 除了它递归合并 sources 来源对象自身和继承的可枚举属性到 object 目标对象。*/
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
console.log(_.merge(object, other));
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }]
/*_.mergeWith(object, sources, customizer)*/
function customizer(objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
var object = { 'a': [1], 'b': [2] };
var other = { 'a': [3], 'b': [4] };
_.mergeWith(object, other, customizer);
// => { 'a': [1, 3], 'b': [2, 4] }
/*_.omit(object, [props]) 删除object的属性*/
var object = { 'a': 1, 'b': '2', 'c': 3 };
console.log(_.omit(object, ['a', 'c']));// => { 'b': '2' }
/*_.omitBy(object, [predicate=_.identity]) */
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.omitBy(object, _.isNumber);// => { 'b': '2' }
/*_.pick(object, [props])创建一个从 object 中选中的属性的对象。*/
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);// => { 'a': 1, 'c': 3 }
/*_.pickBy(object, [predicate=_.identity])*/
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pickBy(object, _.isNumber);// => { 'a': 1, 'c': 3 }
/*_.result(object, path, [defaultValue])这个方法类似 _.get,除了如果解析到的值是一个函数的话,就绑定 this 到这个函数并返回执行后的结果。*/
var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
_.result(object, 'a[0].b.c1');// => 3
_.result(object, 'a[0].b.c2');// => 4
_.result(object, 'a[0].b.c3', 'default');// => 'default'
_.result(object, 'a[0].b.c3', _.constant('default'));// => 'default'
/*_.set(object, path, value) 设置 object对象中对应 path 属性路径上的值,如果path不存在,则创建。 缺少的索引属性会创建为数组,而缺少的属性会创建为对象。 使用 _.setWith 定制path创建。 */
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);// => 4
_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object);
console.log(object.x[0].y.z);// => 5
/*_.setWith(object, path, value, [customizer]) 接受一个 customizer,调用生成对象的 path*/
var object = {};
console.log(_.setWith(object, '[0].[1]', 'a', Object));
/*_.toPairs(object) 创建一个object对象自身可枚举属性的键值对数组。这个数组可以通过_.fromPairs撤回。如果object 是 map 或 set,返回其条目*/
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.toPairs(new Foo);// => [['a', 1], ['b', 2]]
/*_.toPairsIn(object) 创建一个object对象自身和继承的可枚举属性的键值对数组。这个数组可以通过_.fromPairs撤回。如果object 是 map 或 set,返回其条目。*/
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.toPairsIn(new Foo);// => [['a', 1], ['b', 2], ['c', 3]]
/*_.transform(object, [iteratee=_.identity], [accumulator])_.reduce的替代方法;此方法将转换object对象为一个新的accumulator对象,结果来自iteratee处理自身可枚举的属性。 每次调用可能会改变 accumulator 对象。如果不提供accumulator,将使用与[[Prototype]]相同的新对象。*/
_.transform([2, 3, 4], function(result, n) {
result.push(n *= n);
return n % 2 == 0;
}, []);
// => [4, 9]
_.transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
}, {});
// => { '1': ['a', 'c'], '2': ['b'] }
/*_.unset(object, path) 移除object对象 path 路径上的属性*/
var object = { 'a': [{ 'b': { 'c': 7 } }] };
_.unset(object, 'a[0].b.c');// => true
console.log(object);// => { 'a': [{ 'b': {} }] };
_.unset(object, ['a', '0', 'b', 'c']);// => true
console.log(object);// => { 'a': [{ 'b': {} }] };
/*_.update(object, path, updater) 该方法类似_.set,除了接受updater以生成要设置的值。使用 _.updateWith来自定义生成的新path。*/
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.update(object, 'a[0].b.c', function(n) { return n * n; });
console.log(object.a[0].b.c);// => 9
_.update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; });
console.log(object.x[0].y.z);// => 0
/*_.updateWith(object, path, updater, [customizer])*/
var object = {};
_.updateWith(object, '[0][1]', _.constant('a'), Object);
// => { '0': { '1': 'a' } }
/*_.values(object) 创建 object 自身可枚举属性的值为数组。 非对象的值会强制转换为对象。*/
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.values(new Foo);// => [1, 2] (无法保证遍历的顺序)
_.values('hi');// => ['h', 'i']
/*_.valuesIn(object) 创建 object 自身和继承的可枚举属性的值为数组 */
</script>