javascript中lodash里sortby之后的object和原object有什么联系?
我也是稀里糊涂的就实现了这个功能, 但是实在没有明白为什么。
根据打印,lodash 的sortBy之后的结果和原来的obj是完全不一样的,而我的更改是基于sortby之后的结果,为什么obj也跟着变化了?
希望某位知道原因的大神帮忙解惑下?
function test(obj,key) {
console.log("obj_orig======\n%j", obj);
console.log("obj_sortby=====\n%j", _.sortBy(obj, ['age', 'user']));
console.log("obj_after_sortby=====\n%j", obj);
_.sortBy(obj, ['age', 'user']).forEach(function(d, i) { d[key] = (0===i) ? true : false;});
console.log("after_change====\n%j", obj);
}
var users = [
{ 'user': 'Halen', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'Jack', 'age': 34 }
];
test(users ,"zzz");
运行结果输出==========================
obj_orig======
[{"user":"Halen","age":48},{"user":"barney","age":36},{"user":"fred","age":40},{"user":"Jack","age":34}]
obj_sortby=====
[{"user":"Jack","age":34},{"user":"barney","age":36},{"user":"fred","age":40},{"user":"Halen","age":48}]
obj_after_sortby=====
[{"user":"Halen","age":48},{"user":"barney","age":36},{"user":"fred","age":40},{"user":"Jack","age":34}]
after_change====
[{"user":"Halen","age":48,"zzz":false},{"user":"barney","age":36,"zzz":false},{"user":"fred","age":40,"zzz":false},{"user":"Jack","age":34,"zzz":true}]
相关推荐
JavaScript工具库之Lodash详解.docx
JavaScript工具库之Lodash详解.docx
JavaScript工具库之Lodash详解
主要介绍了JavaScript工具库之Lodash详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
ng.lodash:角度使用 lodash
ng.lodash 角度使用 lodash
lodash underscore js库速查手册
Collection Functions (Arrays or Objects) _.each(list, iterator, [context]) Alias: forEach Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is bound to the _.map(list, iterator, [context]) Alias: collect Produces a new array of values by mapping each value in list through a transformation function ( _.reduce(list, iterator, memo, [context]) Aliases: inject, foldl Also known as inject and foldl, reduce boils down a list of values into a single value. _.reduceRight(list, iterator, memo, [context]) Alias: foldr The right-associative version of reduce. Delegates to the JavaScript 1.8 version of _.find(list, iterator, [context]) Alias: detect Looks through each value in the list, returning the first one that passes a truth test (iterator). The function returns as _.filter(list, iterator, [context]) Alias: select Looks through each value in the list, returning an array of all the values that pass a truth test ( _.where(list, properties) Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in _.findWhere(list, properties) Looks through the list and returns the first value that matches all of the key-value pairs listed in properties. _.reject(list, iterator, [context]) Returns the values in list without the elements that the truth test (iterator) passes. The opposite of filter. _.every(list, iterator, [context]) Alias: all Returns true if all of the values in the list pass the iterator truth test. Delegates to the native method _.some(list, [iterator], [context]) Alias: any Returns true if any of the values in the list pass the iterator truth test. Short-circuits and stops traversing the list _.contains(list, value) Alias: include Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. _.invoke(list, methodName, [*arguments]) Calls the method named by methodName on each value in the list. Any extra arguments passed to _.pluck(list, propertyName) A convenient version of what is perhaps the most common use-case for map: extracting a list of property values. _.max(list, [iterator], [context]) Returns the maximum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the _.min(list, [iterator], [context]) Returns the minimum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the _.sortBy(list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator _.groupBy(list, iterator, [context]) Splits a collection into sets, grouped by the result of running each value through iterator. If iterator is a string instead of _.countBy(list, iterator, [context]) Sorts a list into groups and returns a count for the number of objects in each group. Similar to groupBy _.shuffle(list) Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle. _.toArray(list) Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting the arguments object. _.size(list) Return the number of values in the list.
javascript 的几种排序方法
javascript 的几种排序方法


