常用函数

数组中随机取出几个元素

/**
 * 获取指定的参数值
 * @param {*} arr 要处理的数组
 * @param {*} count 需要取出的元素个数
 */
function getRandomArrayElements(arr, count) {
  let shuffled = arr.slice(0)
  let i = arr.length
  let min = i - count
  let temp
  let index
  while (i-- > min) {
    index = Math.floor((i + 1) * Math.random())
    temp = shuffled[index]
    shuffled[index] = shuffled[i]
    shuffled[i] = temp
  }
  return shuffled.slice(min)
}

let items = ['1', '2', '4', '5', '6', '7', '8', '9', '10']

console.log(getRandomArrayElements(items, 4)) // ['1', '2', '5', '6']

数组对象中根据某个相同的属性值合并其他项

/**
* 数组对象中根据某个相同的属性值合并其他项
* @param {arr} arr 要处理的数组
* @param {string} id 筛选的相同对象
*/
function getArrObject(arr, id) {
  let tempArr = [], newArr = []
  for (let i = 0; i < arr.length; i++) {
    if (tempArr.indexOf(arr[i].id) === -1) {
      newArr.push({
        id: arr[i].id,
        list: [arr[i]]   // list:按照相同id,合并其他项的数组,包含id本身
      })
      tempArr.push(arr[i].id);
    } else {
      for (let j = 0; j < newArr.length; j++) {
        if (newArr[j].id == arr[i].id) {
          newArr[j].list.push(arr[i])
        }
      }
    }
  }
  return newArr
}

let arr = [
{ id: 1, list: { id: 66 } },
{ id: 1, list: { id: 44 } },
{ id: 2, list: { id: 55 } },
{ id: 2, list: { id: 33 } },
{ id: 3, list: { id: 11 } },
{ id: 3, list: { id: 22 } }
]

console.log(getArrObject(arr, 'id'))

getArrObject(arr, 'id')= [
  ({
    id: 1,
    newlist: [
      { id: 1, list: { id: 66 } },
      { id: 1, list: { id: 44 } },
    ],
  },
  {
    id: 2,
    newlist: [
      { id: 2, list: { id: 55 } },
      { id: 2, list: { id: 33 } },
    ],
  },
  {
    id: 3,
    newlist: [
      { id: 3, list: { id: 11 } },
      { id: 3, list: { id: 22 } },
    ],
  }),
]

语音合成

不需要网络,离线 JavaScript 语音合成

<!doctype html>
<html lang="zh">

<body>
  <button onclick="speak();">语音合成</button>
  <script>
    function speak () {
      var utterThis = new window.SpeechSynthesisUtterance()
      utterThis.lang = "zh-CN";
      utterThis.text = "这是一段测试用的语音";
      window.speechSynthesis.speak(utterThis)
    }
    speak();
  </script>
</body>

</html>

生成某个范围的随机数【四种情况详解】

min ≤ r ≤ max (一般这种比较常见)

function RandomNumBoth(Min, Max) {
  var Range = Max - Min
  var Rand = Math.random()
  var num = Min + Math.round(Rand * Range) //四舍五入
  return num
}

min ≤ r < max

function RandomNum(Min, Max) {
  var Range = Max - Min
  var Rand = Math.random()
  var num = Min + Math.floor(Rand * Range) //舍去
  return num
}

min < r ≤ max

function RandomNum(Min, Max) {
  var Range = Max - Min
  var Rand = Math.random()
  if (Math.round(Rand * Range) == 0) {
    return Min + 1
  }
  var num = Min + Math.round(Rand * Range)
  return num
}

min < r < max

function RandomNum(Min, Max) {
  var Range = Max - Min
  var Rand = Math.random()
  if (Math.round(Rand * Range) == 0) {
    return Min + 1
  } else if (Math.round(Rand * Max) == Max) {
    index++
    return Max - 1
  } else {
    var num = Min + Math.round(Rand * Range) - 1
    return num
  }
}
上次更新:
作者: 洛埋名