我正在学习JavaScript,并在网站上遇到过这个功能。它似乎使用jQuery,我没有太多的经验。我理解除了$.each
的行以外的函数。
你可以帮助我重写没有$.each
的功能吗?即使在阅读了jQuery文档之后,我也不明白这意味着什么。它是指什么,如何在不使用jQuery的情况下重写此函数?
ArraySort = function(array, sortFunc){
var tmp = [];
var aSorted=[];
var oSorted={};
for (var k in array) {
if (array.hasOwnProperty(k))
tmp.push({key: k, value: array[k]});
}
tmp.sort(function(o1, o2) {
return sortFunc(o1.value, o2.value);
});
if(Object.prototype.toString.call(array) === '[object Array]'){
$.each(tmp, function(index, value){
aSorted.push(value.value);
});
return aSorted;
}
if(Object.prototype.toString.call(array) === '[object Object]'){
$.each(tmp, function(index, value){
oSorted[value.key]=value.value;
});
return oSorted;
}
};
答案 0 :(得分:3)
您可以从
更新代码$.each(tmp, function(index, value){
aSorted.push(value.value);
});
到
for (var i = 0; i < tmp.length; i++) {
aSorted.push(tmp[i].value);
}
同样,您可以通过更新到以下
来为其他$.each
实现相同的目标
for (var i = 0; i < tmp.length; i++) {
var obj = tmp[i];
oSorted[obj.key] = obj.value;
};
注意:tmp已排序。您只能返回tmp,然后迭代它并获取tmp.value或tmp.key。
答案 1 :(得分:2)
在这两种情况下,$.each
仅用于遍历tmp
中的数组条目。
This answer有一个详尽的列表,列出了在不使用任何库的情况下循环JavaScript中的数组条目的选项。对于当前的浏览器,通常的方式是Array#forEach
:
tmp.forEach(function(value, index) {
// ...
});
请注意,回调的参数顺序不同。但同样,其他答案中列出了其他选项。事实上,在使用forEach
时,我们不需要index
广告,因为$.each
循环中的代码并未使用它,它是就在那里因为它是第一个参数而代码想要第二个参数(value
)。所以我们可以放弃它:
tmp.forEach(function(value) {
// ...
});
在原始代码中使用了$.each
的每个地方,如果您只是将$.each
调用的正文放入上面的// ...
,那么它会做同样的事情$.each
正在做。
因此,对于第一次调用(在=== '[object Array]'
分支中),看起来像这样:
tmp.forEach(function(value){
aSorted.push(value.value);
});
对于第二次调用(在=== '[object Object]'
分支中),如下所示:
tmp.forEach(function(value) {
oSorted[value.key]=value.value;
});
虽然上述内容足以摆脱$.each
,但这里是一个完全重写的版本,带有解释性文字:
ArraySort = function(array, sortFunc){
// If `array` is an array, this is trivial: Copy it and use `Array#sort`
// (since the original created a copy)
if (Array.isArray(array)) {
return array.slice().sort(sortFunc);
}
// If `array` is not a plain object, return `undefined`
// (This is effectively what the original did, by simply
// falling off the bottom of the function.)
if (object.prototype.toString.call(array) !== "[object Object]") {
return undefined;
}
// Create a "sorted" version of the object.
// NOTE!!! In the current version of JavaScript, object properties HAVE
// NO ORDER. At all. None. However, in the next version of JavaScript,
// ECMAScript6 (aka "ES6"), they will have an order, and V8 (the engine
// in NodeJS) already supports that order. The order is based on the
// order in which the properties were created (but it's more complicated
// that that, see:
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
// Get an array of objects with `key` being the property name and
// `value` being its value.
var tmp = Object.keys(array).map(function(key) {
return {key: key, value: array[key]};
});
// Sort `tmp` according to `sortFunc` on its values
tmp.sort(function(o1, o2) {
return sortFunc(o1.value, o2.value);
});
// Create and return a new object with the properties in that order (see
// caveat above!!)
var result = {};
tmp.forEach(function(entry) {
result[entry.key] = entry.value;
});
return result;
};
答案 2 :(得分:-1)
你是对的,这是jQuery。它意味着:“对于对象tmp中的每个元素来说,功能”就是它的含义。
我不会尝试将其转换为javascript,我会采取额外步骤并学习jQuery,但它非常强大!
在Javascript中:
for(var i = 0; i < tmp.length ; i++){
aSorted.push(tmp[i].value);
}
和第二个$ .each
for(var i in tmp){
oSorted[i]=tmp[i].value;
}