您可以通过3种内置阵列功能实现此目的:concat,filter和sort。
注:我已经建立了这个代码,假设你想改变过滤和排序的条件。因此,我将所有功能分成了不同的功能,但如果条件总是相同,则可以简化主功能。
首先,你要过滤。因此,我们需要一个函数,当它符合你想保持你不想要的那些元素和假的条件返回true:
/**
* Function defined to decide how to filter our objects.
* @param {!Object} element - Object to filter.
* @return {boolean} Whether it meets all the conditions.
*/
var filterFunction = function(element) {
// Define all conditions by which to filter.
return element.loaded === false;
/*
* Other example: return objects whose attributes meet these conditions:
* ordered_position lesser than five
* AND loaded === true;
*
* return element.ordered_position <= 5 && element.loaded;
*/
}
按照你的例子,这个人将只包含元素其加载的属性为false。您可以添加更多条件或根据您的意愿对每次执行进行更改。
然后,我们需要一个分选功能:
/**
* Function to order objects by parameters.
* @param {Object} a - First object in comparison.
* @param {Object} b - Second object in comparison.
* @return {number} Order: negative (< 0), positive (> 0) or neutral (0).
*/
var orderFunction = function(a, b) {
// Order by ordered_position ASC.
return a.ordered_position - b.ordered_position;
}
这一个订单由ordered_position ASC如同先前的值(A)>下一个值(b)中,它返回一个正值。您可以通过反转参数来执行DESC,或者通过多个参数构建您自己的排序函数来排序字符串或甚至排序。
最后,我们可以放在一起我们的工作流程:
/**
* Gets all the arrays, filters by one parameter,
* returns data ordered by another parameter as an Array.
* @param {!Array<Array>} arrays - Group of arrays to search in.
* @param {Function} opt_filterBy - Function with filtering conditions.
* @param {Function} opt_orderBy - Parameter for which to order.
* @return {Array<Object>} Merged, filtered and sorted Array.
*/
var arrayFilter = function (arrays, opt_filterBy, opt_orderBy) {
// Merge all arrays in one.
var mergedArray = [].concat.apply([], arrays);
// Iterate array to remove elements.
if (opt_filterBy) var filteredArray = mergedArray.filter(opt_filterBy);
// Sort array by parameter.
if (opt_orderBy) filteredArray = filteredArray.sort(opt_orderBy);
// return Merged, filtered and sorted Array.
return filteredArray;
}
该函数有3个参数:您的数组的数组(所有要合并的阵列,筛选和排序)功能来过滤(可选,我们定义的第一个)和一个排序函数(可选,我们定义的第二个函数)。
若要将此到你的情况,你可以运行这样的:
var mandatorySections = [
{ordered_position:0,loaded:false},
{ordered_position:2,loaded:false},
{ordered_position:4,loaded:true},
{ordered_position:8,loaded:false}
];
var optionnalSections = [
{ordered_position:3,loaded:false}
];
arrayFilter(
[mandatorySections, optionnalSections],
filterFunction,
orderFunction
);
/**
* Function defined to decide how to filter our objects.
* @param {!Object} element - Object to filter.
* @return {boolean} Whether it meets all the conditions.
*/
var filterFunction = function(element) {
// Define all conditions by which to filter.
return element.loaded === false;
/*
* Other example: return objects whose attributes meet these conditions:
* ordered_position lesser than five
* AND loaded === true;
*
* return element.ordered_position <= 5 && element.loaded;
*/
}
/**
* Function to order objects by parameters.
* @param {Object} a - First object in comparison.
* @param {Object} b - Second object in comparison.
* @return {number} Order: negative (< 0), positive (> 0) or neutral (0).
*/
var orderFunction = function(a, b) {
// Order by ordered_position ASC.
return a.ordered_position - b.ordered_position;
}
/**
* Gets all the arrays, filters by one parameter,
* returns data ordered by another parameter as an Array.
* @param {!Array<Array>} arrays - Group of arrays to search in.
* @param {Function} opt_filterBy - Function with filtering conditions.
* @param {Function} opt_orderBy - Parameter for which to order.
* @return {Array<Object>} Merged, filtered and sorted Array.
*/
var arrayFilter = function (arrays, opt_filterBy, opt_orderBy) {
// Merge all arrays in one.
var mergedArray = [].concat.apply([], arrays);
// Iterate array to remove elements.
if (opt_filterBy) var filteredArray = mergedArray.filter(opt_filterBy);
// Sort array by parameter.
if (opt_orderBy) filteredArray = filteredArray.sort(opt_orderBy);
// return Merged, filtered and sorted Array.
return filteredArray;
}
/* Execution of code. */
var mandatorySections = [
{ordered_position:0,loaded:false},
{ordered_position:2,loaded:false},
{ordered_position:4,loaded:true},
{ordered_position:8,loaded:false}
];
var optionnalSections = [
{ordered_position:3,loaded:false}
];
var result = arrayFilter(
[mandatorySections, optionnalSections],
filterFunction,
orderFunction
);
console.log(result);
我试图保持这个一般的,可扩展的,但你可以很容易地简化如果代码你的用例更基本。
我希望这会有所帮助。
你已经描述了你想要什么,但没有显示你所尝试的或你对过程有什么具体问题。 – csmckelvey
合并两个数组,按“已加载”键过滤,然后按“ordered_position”键进行排序。你不知道该怎么做? – JJJ