首页 > 技术文章 > 常用js代码片段(三)

jmwlhj 2020-07-14 10:00 原文

57、按照给定的函数对比两个数组的差异,然后找出交集,最后从第一个数组中将对应的元素输出。

const intersectionWith = (a, b, comp) => a.filter(x => b.findIndex(y => comp(x, y)) !== -1);

intersectionWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)); // [1.5, 3, 0]

58、此段代码用于判断数据是否为指定的数据类型,如果是则返回true。

const is = (type, val) => ![, null].includes(val) && val.constructor === type;

is(Array, [1]); // true
is(ArrayBuffer, new ArrayBuffer()); // true
is(Map, new Map()); // true
is(RegExp, /./g); // true
is(Set, new Set()); // true
is(WeakMap, new WeakMap()); // true
is(WeakSet, new WeakSet()); // true
is(String, ''); // true
is(String, new String('')); // true
is(Number, 1); // true
is(Number, new Number(1)); // true
is(Boolean, true); // true
is(Boolean, new Boolean(true)); // true

59、接收两个日期类型的参数,判断前者的日期是否晚于后者的日期。

const isAfterDate = (dateA, dateB) => dateA > dateB;

isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true

60、用于检测两个单词是否相似。

const isAnagram = (str1, str2) => {
  const normalize = str =>
    str
      .toLowerCase()
      .replace(/[^a-z0-9]/gi, '')
      .split('')
      .sort()
      .join('');
  return normalize(str1) === normalize(str2);
};

isAnagram('iceman', 'cinema'); // true

61、此段代码用于检测对象是否为类数组对象,是否可迭代。

const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function';

isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false

62、接收两个日期类型的参数,判断前者的日期是否早于后者的日期。

const isBeforeDate = (dateA, dateB) => dateA < dateB;

isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true

63、此段代码用于检查参数是否为布尔类型。

const isBoolean = val => typeof val === 'boolean';isBoolean(null); // falseisBoolean(false); // true

64、用于判断程序运行环境是否在浏览器,这有助于避免在node环境运行前端模块时出错。

const isBrowser = () => ![typeof window, typeof document].includes('undefined');

isBrowser(); // true (browser)
isBrowser(); // false (Node)

65、用于判断当前页面是否处于活动状态(显示状态)。

const isBrowserTabFocused = () => !document.hidden;
isBrowserTabFocused(); // true

66、用于判断当前字符串是否都为小写。

const isLowerCase = str => str === str.toLowerCase();

isLowerCase('abc'); // true
isLowerCase('a3@$'); // true
isLowerCase('Ab4'); // false

67、用于判断当前变量的值是否为 null 或 undefined 类型。

const isNil = val => val === undefined || val === null;

isNil(null); // true
isNil(undefined); // true

68、用于判断当前变量的值是否为 null 类型。

const isNull = val => val === null;

isNull(null); // true

69、用于检查当前的值是否为数字类型。

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

isNumber('1'); // false
isNumber(1); // true

70、用于判断参数的值是否是对象,这里运用了Object 构造函数创建一个对象包装器,如果是对象类型,将会原值返回。

const isObject = obj => obj === Object(obj);

isObject([1, 2, 3, 4]); // true
isObject([]); // true
isObject(['Hello!']); // true
isObject({ a: 1 }); // true
isObject({}); // true
isObject(true); // false

71、用于检查参数的值是否为null以及类型是否为对象。

const isObjectLike = val => val !== null && typeof val === 'object';

isObjectLike({}); // true
isObjectLike([1, 2, 3]); // true
isObjectLike(x => x); // false
isObjectLike(null); // false

72、此代码段检查参数的值是否是由Object构造函数创建的对象。

const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;

isPlainObject({ a: 1 }); // true
isPlainObject(new Map()); // false

73、用于检查当前的对象是否类似Promise函数。

const isPromiseLike = obj =>
  obj !== null &&
  (typeof obj === 'object' || typeof obj === 'function') &&
  typeof obj.then === 'function';
  
isPromiseLike({
  then: function() {
    return '';
  }
}); // true
isPromiseLike(null); // false
isPromiseLike({}); // false

74、用于判断给定的两个日期是否是同一天。

const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();

isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true

75、用于检查当前的值是否为字符串类型。

const isString = val => typeof val === 'string';

isString('10'); // true

76、用于判断参数的值是否是 Symbol 类型。

const isSymbol = val => typeof val === 'symbol';

isSymbol(Symbol('x')); // true

77、用于判断参数的类型是否是 Undefined 类型。

const isUndefined = val => val === undefined;

isUndefined(undefined); // true

78、用于判断当前字符串的字母是否都为大写。

const isUpperCase = str => str === str.toUpperCase();

isUpperCase('ABC'); // true
isLowerCase('A3@$'); // true
isLowerCase('aB4'); // false

79、用于判断给定的字符串是否是 JSON 字符串。

const isValidJSON = str => {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
};

isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // true

80、此函数功能返回数组的最后一个元素。

const last = arr => arr[arr.length - 1];

last([1, 2, 3]); // 3

81、此函数功能用于比较两个对象,以确定第一个对象是否包含与第二个对象相同的属性与值。

const matches = (obj, source) =>
  Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]);
  
matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true
matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false

82、此代码段查找日期数组中最大的日期进行输出。

const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates));

const array = [
  new Date(2017, 4, 13),
  new Date(2018, 2, 12),
  new Date(2016, 0, 10),
  new Date(2016, 0, 9)
];
maxDate(array); // 2018-03-11T22:00:00.000Z

83、此段代码输出数组中前 n 位最大的数。

const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);

maxN([1, 2, 3]); // [3]
maxN([1, 2, 3], 2); // [3,2]

84、此代码段查找日期数组中最早的日期进行输出。

const minDate = (...dates) => new Date(Math.min.apply(null, ...dates));

const array = [
  new Date(2017, 4, 13),
  new Date(2018, 2, 12),
  new Date(2016, 0, 10),
  new Date(2016, 0, 9)
];
minDate(array); // 2016-01-08T22:00:00.000Z

 

推荐阅读