Added more Array methods
This commit is contained in:
parent
52fb34f04d
commit
4ffccd8b1e
|
@ -0,0 +1,47 @@
|
|||
<!doctype html>
|
||||
<html lang="en-GB">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="content-type" content="text/html" charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="" />
|
||||
<meta name="keyword" content="" />
|
||||
|
||||
<script src="ryjsxt.js"></script>
|
||||
<!-- <script src="bsdialog4.min.js"></script> -->
|
||||
<title></title>
|
||||
|
||||
<style>
|
||||
|
||||
.text-sm {
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
<script>
|
||||
|
||||
var dataSet = [
|
||||
{ key: "b", value: "B2", num: 2, f: 2.2, date: new Date(2001, 2, 2) },
|
||||
{ key: "d", value: "D4", num: 4, f: 4.4, date: new Date(2001, 4, 4) },
|
||||
{ key: "a", value: "A1", num: 1, f: 1.1, date: new Date(2001, 10, 1) },
|
||||
{ key: "e", value: "E5", num: 5, f: 5.5, date: new Date(2001, 5, 5) },
|
||||
{ key: "c", value: "C3", num: 3, f: 3.3, date: new Date(2001, 3, 3) }
|
||||
];
|
||||
|
||||
// let result = dataSet.index("key", "d");
|
||||
//dataSet = dataSet.orderBy("date");
|
||||
dataSet = dataSet.orderByDesc("date");
|
||||
|
||||
// console.log(result);
|
||||
console.log(dataSet);
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
108
ryjsxt.js
108
ryjsxt.js
|
@ -4,6 +4,20 @@
|
|||
*/
|
||||
|
||||
|
||||
Array.prototype.index = function (propName, value) {
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (typeof(this[i][propName]) == "undefined") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this[i][propName] == value){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
};
|
||||
|
||||
Array.prototype.insert = function(index, item) {
|
||||
if (index < 0) {
|
||||
this.splice(0, 0, item);
|
||||
|
@ -36,6 +50,64 @@ Array.prototype.joinIfNotNullOrWhitespace = function (separator) {
|
|||
return result;
|
||||
};
|
||||
|
||||
Array.prototype.first = function (propName, value) {
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (typeof(this[i][propName]) == "undefined") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this[i][propName] == value){
|
||||
return this[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
Array.prototype.indexes = function (propName, value) {
|
||||
let result = [];
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (typeof(this[i][propName]) == "undefined") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this[i][propName] == value){
|
||||
result.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
Array.prototype.orderBy = function (propName) {
|
||||
this.sort(function(a, b) {
|
||||
if (a[propName] < b[propName]) {
|
||||
return -1;
|
||||
} else if (a[propName] > b[propName]) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
Array.prototype.orderByDesc = function (propName) {
|
||||
this.sort(function(a, b) {
|
||||
if (a[propName] < b[propName]) {
|
||||
return 1;
|
||||
} else if (a[propName] > b[propName]) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
Array.prototype.removeAt = function(index) {
|
||||
if ((index < 0) || (index >= this.length)) {
|
||||
return this;
|
||||
|
@ -46,6 +118,22 @@ Array.prototype.removeAt = function(index) {
|
|||
return this;
|
||||
};
|
||||
|
||||
Array.prototype.select = function (propName, value) {
|
||||
let result = [];
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (typeof(this[i][propName]) == "undefined") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this[i][propName] == value){
|
||||
result.push(this[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -176,6 +264,26 @@ String.isNullOrWhitespace = function(value) {
|
|||
}
|
||||
};
|
||||
|
||||
String.joinIfNotNullOrWhitespace = function (separator, ...values) {
|
||||
const a = this;
|
||||
|
||||
let result = "";
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
if (String.isNullOrWhitespace(values[i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!String.isNullOrWhitespace(result)) {
|
||||
result += separator;
|
||||
}
|
||||
|
||||
result += values[i];
|
||||
};
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
String.prototype.contains = function(...args) {
|
||||
for (let arg of args) {
|
||||
if (this == arg) {
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue