1. 在一个数组对象中, 删除对象键值对重复的元素
/**
* 判断数组对象中是否有某个值
* @param {*} arr 表示要遍历的数组
* @param {*} key 表示用数组对象中哪个键来查询重复数据并删除
*/
function distinct3(arr, key) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i][key] === arr[j][key]) {
arr.splice(j, 1)
j = j - 1
}
}
}
return arr
}
2.判断数组对象中是否含有某个值
/**
* @param {*} array 要查询的数组
* @param {*} attr 要查询的字段
* @param {*} val 要查询的字段值
*/
function findElem(array, attr, val) {
for (var i = 0; i < array.length; i++) {
if (array[i][attr] == val) {
return i; //如果有,返回当前索引值
}
}
return -1;
};
3.获取script标签内的GLSL ES字符串
<script id="vs" type="text/glsl">
attribute vec3 v3Position;
void main(void)
{
gl_Position = vec4(v3Position, 1.0);
}
</script>
<script id="fs" type="text/glsl">
void main(void)
{
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
<script>
/** @type {HTMLCanvasElement} */
/**
* @param {*} scriptID script标签的id
*/
function getShaderShource(scriptID) {
let shaderScript = document.getElementById(scriptID);
if (shaderScript == null) return "";
let sourceCode = '';
let child = shaderScript.firstChild;
while (child) {
if (child.nodeType == child.TEXT_NODE) sourceCode += child.textContent;
child = child.nextSibling;
}
return sourceCode;
}
<script>
4. 增加年月日时间中的日
处理格式为: 2021-4-17
/**
* @param {*} String 要增加的年月日
* @param {*} Number 要增加的天数
*/
getM(M, D) {
getM(M, D) {
let time = M.split("-")
for (let i = 0; i < time.length; i++) {
time[i] = parseInt(time[i])
}
// time[0] = 2021
// time[1] = 12
// time[2] = 25
time[2] = time[2] + D //增加后的日
let bigM =
time[1] == 1 ||
time[1] == 3 ||
time[1] == 5 ||
time[1] == 7 ||
time[1] == 8 ||
time[1] == 10 ||
time[1] == 12 //大月,31天
let sM = time[1] == 4 || time[1] == 6 || time[1] == 9 || time[1] == 11
let runM = time[0] % 4 == 0 // 闰年 && time[1] == 2
if (bigM) {
// console.log("大月天数")
if (time[2] > 31) {
time[2] = time[2] - 31
console.log(time[2])
time[1] = time[1] + 1
}
}
if (sM) {
// console.log("小月天数")
if (time[2] > 30) {
time[2] = time[2] - 30
time[1] = time[1] + 1
}
}
if (runM && time[1] == 2) {
// console.log("润2月天数")
if (time[2] > 28) {
time[2] = time[2] - 28
time[1] = time[1] + 1
}
}
if (!runM && time[1] == 2) {
// console.log("平2月天数")
if (time[2] > 29) {
time[2] = time[2] - 29
time[1] = time[1] + 1
}
}
if (time[1] > 12) {
// console.log("跨年月")
time[1] = time[1] - 12
time[0] = time[0] + 1
}
return time.join("-")
},