📄 array.js
字号:
return -1; } findKeys[keyField] = record[keyField]; } if (!hasKeys) { //>DEBUG isc.Log.logWarn("findByKeys: dataSource '" + dataSource.ID + "' does not have primary " + "keys declared, can't find record"); //<DEBUG return -1; } // go through the recordSet looking for a record with the same values for the primary keys return this.findNextIndex(pos, findKeys, null, endPos);},//> @method array.containsProperty()// Determine whether this array contains any members where the property passed in matches the value// passed in.//// @group find// @param property (string) property to look for// (object) key:value pairs to look for// @param [value] (any) value to compare against (if property is a string)//// @return (boolean) true if this array contains an object with the appropriate property value// @visibility external//<containsProperty : function (property, value) { var index = this.findIndex(property, value); return (index != -1);},//> @method array.findAll()// @include list.findAll//<findAll : function (property, value) { if (property == null) return null; if (isc.isA.String(property)) { var matches = null, l = this.length ; // single property to match for (var i = 0; i < l; i++) { var item = this[i]; if (item && item[property] == value) { if (matches == null) matches = []; matches.add(item); } } return matches; } else { // "property" is an object specifying a set of properties to match return this.findAllMatches(property); }},// internal: assumes multiple propertiesfindAllMatches : function (properties) { var l = this.getLength(), propertyNames = isc.getKeys(properties), matches = null ; for (var i = 0; i < l; i++) { var item = this.get(i); if (!item) continue; var found = true; for (var j = 0; j < propertyNames.length; j++) { var propertyName = propertyNames[j]; if (item[propertyName] != properties[propertyName]) { found = false; break; } } if (found) { if (matches == null) matches = []; matches.add(item); } } return matches;},//> @method array.slide() (A)// Slide element at position start to position destination, moving all the other elements to cover// the gap.//// @param start (number) start position// @param destination (number) destination position for this[start]// @visibility external//<slide : function (start, destination) { this.slideRange(start, start+1, destination);},//> @method array.slideRange() (A)// Slide a range of elements from start to end to position destination, moving all the other// elements to cover the gap.//// @param start (number) start position// @param end (number) end position (exclusive, like substring() and slice())// @param destination (number) destination position for the range// @visibility external//<slideRange : function (rangeStart, rangeEnd, destination) { // remove the range to be moved var removed = this.splice(rangeStart, rangeEnd - rangeStart); // and add it at the destination this.addListAt(removed, destination);},//> @method array.slideList() (A)// Slide the array of rows list to position destination.//// @param start (number) start position// @param destination (number) destination position for this[start]//<slideList : function (list, destination) { var output = [], i ;//XXX if destination is negative, set to 0 (same effect, cleaner code below)if (destination < 0) destination = 0; // take all the things from this table before destination that aren't in the list to be moved for(i = 0;i < destination;i++) if (!list.contains(this[i])) output.add(this[i]); // now put in all the things to be moved for(i = 0;i < list.length;i++) output.add(list[i]); // now put in all the things after destination that aren't in the list to be moved for(i = destination;i < this.length;i++) if (!list.contains(this[i])) output.add(this[i]); // now copy the reordered list back into this array this.setArray(output);},//> @method array.makeIndex() (A)// Make an index for the items in this Array by a particular property of each item.// <P>// Returns an Object with keys for each distinct listItem[property] value. Each key will point// to an array of items that share that property value. The sub-array will be in the same order// that they are in this list.//// @param property (strings) names of the property to index by// @param alwaysMakeArray (boolean : false) // if true, we always make an array for every index. if false, we make an Array only// when more than one item has the same value for the index property// @return (object) index object// @visibility external//<// NOTE: we don't document the awkard -1 param to allow collisionsmakeIndex : function (property, alwaysMakeArray, useIndexAsKey) { var index = {}; var allowCollisions = (alwaysMakeArray == -1); alwaysMakeArray = (alwaysMakeArray != null && alwaysMakeArray != 0); for (var i = 0; i < this.length; i++) { var item = this[i], key = item[property] ; // if the item has no value for the key property if (key == null) { // either skip it.. if (!useIndexAsKey) continue; // or place it in the index under its position in the array key = i; } if (allowCollisions) { index[key] = item; continue; } var existingValue = index[key]; if (existingValue == null) { if (alwaysMakeArray) { // every entry should be an array index[key] = [item]; } else { index[key] = item; } } else { if (alwaysMakeArray) { // any non-null value is an array we created the first time we found an item // with this key value index[key].add(item); } else { // if the existing value is an array, add to it, otherwise put the new and old // value together in a new array var existingValue = index[key]; if (isc.isAn.Array(existingValue)) { index[key].add(existingValue); } else { index[key] = [existingValue, item]; } } } } return index; },//> @method array.arraysToObjects() (A)// Map an array of arrays to an array of objects.// <P>// Each array becomes one object, which will have as many properties as the number of property// names passed as the "propertyList". The values of the properties will be the values found// in the Array, in order.// <P>// For example:// <pre>// var arrays = [// ["Mickey", "Mouse"],// ["Donald", "Duck"],// ["Yosemite", "Sam"]// ];// var objects = arrays.arraysToObjects(["firstName", "lastName"]);// </pre>// <code>objects</code> is now:// <pre>// [// { firstName:"Mickey", lastName:"Mouse" },// { firstName:"Donald", lastName:"Duck" },// { firstName:"Yosemite", lastName:"Sam" }// ]// </pre>//// @param propertyList (Array of String) names of the properties to assign to//// @return (Array of Object) corresponding array of objects//<arraysToObjects : function (propertyList) { // get the number of properties we're dealing with var propLength = propertyList.length; // for each item in this array for (var output = [], i = 0, l = this.length; i < l; i++) { // make a new object to hold the output var it = output[i] = {}; // for each property in the propertyList list for (var p = 0; p < propLength; p++) { var property = propertyList[p]; // assign that item in the array to the proper name of the new object it[property] = this[i][p]; } } // return the list that was generated return output;},//> @method array.objectsToArrays() (A)// Map an array of objects into an array of arrays.// <P>// Each object becomes one array, which contains the values of a list of properties from// the source object.// <P>// For example:// <pre>// var objects = [// { firstName:"Mickey", lastName:"Mouse" },// { firstName:"Donald", lastName:"Duck" },// { firstName:"Yosemite", lastName:"Sam" }// ]// var arrays = objects.objectsToArrays(["firstName", "lastName"]);// </pre>// <code>arrays</code> is now:// <pre>// [// ["Mickey", "Mouse"],// ["Donald", "Duck"],// ["Yosemite", "Sam"]// ]// </pre>//// @param propertyList (Array of String) names of the properties to output//// @return (Array of Object) corresponding array of arrays//<objectsToArrays : function (propertyList) { // get the number of properties we're dealing with var propLength = propertyList.length; // for each item in this array for (var output = [], i = 0, l = this.length; i < l; i++) { // make a new object to hold the output var it = output[i] = []; // for each property in the propertyList list for (var p = 0; p < propLength; p++) { var property = propertyList[p]; // assign that item in the array to the proper name of the new object it[p] = this[i][property]; } } // return the list that was generated return output; },//> @method array.spliceArray() // Like array.splice() but takes an array (to concat) as a third parameter,// rather than a number of additional parameters.//// @param startPos (number) starting position for the splice// @param deleteCount (number) Number of elements to delete from affected array// @param newArray (any[]) Array of elements to splice into existing array//// @return (any[]) array of removed elements//<spliceArray : function (startPos, deleteCount, newArray) { var undefined; if (startPos === undefined) return this.splice(); if (deleteCount === undefined) return this.splice(startPos); if (newArray === undefined) return this.splice(startPos, deleteCount); if (!isc.isAn.Array(newArray)) { isc.Log.logWarn("spliceArray() method passed a non-array third parameter. Ignoring...", "Array"); return this.splice(startPos, deleteCount); } // use 'apply' - allows you to pass in the arguments as an array! // xxx - // Note 1: Another syntax for this would be of this form // if(newArray.length <= 10) return this.splice(startPos, deleteCount, newArray[0], ...) // else return this.splice.apply(...) // but seems no better performance-wise, and since (at least in our overridden implementation of // splice for IE 5.0) we use arguments.length, is unreliable unless we have a plethora of // if ... then/s to pass in exactly the right number of arguments. // // Note 2: you have to use concat, rather than splice to put startPos / deleteCount at the // beginning of newArray, as newArray points to an array object that may be being reused // elsewhere, so we can't modify it. // return this.splice.apply(this, [startPos, deleteCount].concat(newArray)) },// stack peek method - returns the top item on the stack without removing it.peek : function () { var item = this.pop(); this.push(item); return item;},//// ----------------------------------------------------------------------------------// add the observation methods to the Array.prototype as well so we can use 'em there//observe: isc.Class.getPrototype().observe, ignore : isc.Class.getPrototype().ignore,_makeNotifyFunction : isc.Class.getPrototype()._makeNotifyFunction,// Synonyms and backcompat// -------------------------------------------------------------------------------------------- //>!BackCompat 2004.6.15 for old ISC names removeItem : function (pos) { return this.removeAt(pos) }, getItem : function (pos) { return this.get(pos) }, setItem : function (pos) { return this.set(pos) }, // NOTE: instead of calling clearAll(), setLength(0) should be called (which is much more // efficient), however clearAll() still exists to support the old behavior of returning the // removed items. clearAll : function (list) { return this.removeList(this) }, //<!BackCompat // Support for java.util.List API size : function () { return this.getLength() }, subList : function (start, end) { return this.getRange(start, end) }, addAll : function (list) { return this.addList(list); }, removeAll : function (list) { var origLength = this.getLength(); this.removeList(list); return this.getLength() != origLength; // return whether list was changed }, clear : function () { this.setLength(0); }, toArray : function () { return this.duplicate(); } // NOTE: incomplete compatibility: // - no iterators. This exists in Java largely for concurrent modification reasons. // - remove(int): in Java, the collision between remove(int) and remove(object) is // implemented by method overloading. In JS, we assume if you pass a number you want // removal by index, but this means remove(5) cannot be used to remove the first instance // of the number 5 from our List. // - retainAll: not yet implemented. Similar to intersect, except the Java version // requires the List to change in place instead of returning the intersection, in order // to preserve the target List's class. // - toArray(): in Java, this means go to a native, non-modifiable Array});// Fixes to splice() in older browsers.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -