Giter VIP home page Giter VIP logo

Comments (7)

menglala avatar menglala commented on July 21, 2024 8

随机打乱数组:

function shuffle(arr) {
  arr.sort(() => {
    return Math.random() - 0.5
  })
}

from frontend-interview.

Caijialinxx avatar Caijialinxx commented on July 21, 2024 1
  • 现在有一个二维数组,数组长度和子数组长度均不可知,问:从每一个子数组里面取一个数成一个数组,要求组合不能重复,问一共有多少个组合,请设计一个函数把所有可能出现的组合以数组的形式输出。

这个方法的适用范围为:二维数组中子数组的每个元素都是数字、重新组合不考虑前后顺序。
如果子数组中可以有字符串,那么 removeSame() 可能不适用。因为若其中一个子数组为 [1, '1'] ,hash[1]hash['1'] 会被认为相同。

function reCombine(array) {
  if (array.length > 1) {
    let recombinedArray = []
    for(let i = 0; i < array[0].length; i++) {
      for(let j = 0; j < array[1].length; j++) {
        recombinedArray.push([].concat(array[0][i], array[1][j]))
      }
    }
    recombinedArray = removeSame(recombinedArray)
    array.splice(0, 2, recombinedArray)
    reCombine(array)
  }
  return array[0]
}

function removeSame(array) {
  let newArray = [], hash = {}
  for (let i = 0; i < array.length; i++) {
    if (!hash[array[i]]) {
      hash[array[i]] = true
      newArray.push(array[i])
    }
  }
  return newArray
}

JSBin在线演示

from frontend-interview.

liyuanqiu avatar liyuanqiu commented on July 21, 2024 1

随机打乱数组:

function obfs(arr) {
  return arr.slice().reduce(acc => [...acc, ...arr.splice(parseInt(Math.random() * arr.length, 10), 1)], []);
}

console.log(obfs([1, 2, 3, 4, 5]));

from frontend-interview.

Caijialinxx avatar Caijialinxx commented on July 21, 2024
  • 如何随机打乱一个js数组
/* 
 * Returns a new array after disordered.
 * 
 * @param {array} array - The original array needs to be disordered.
 * @returns {array} newArray - A new array after disordered.
 *
 * @author Caijialinxx
 */
function disorderArray(array) {
  let newArray = []
  let usedIndexs = []    // Put the index of item which had been pushed into newArray. It helps to avoid adding repeated item.
  let validCount = 0     // The count of valid pushing into newArray.
  top:
  while(validCount < array.length) {
    let index = Math.floor(Math.random()*array.length)
    if(usedIndexs.indexOf(index) > -1) {
      continue top
    } else {
      usedIndexs.push(index)
    }
    validCount++
    newArray.push(array[index])
  }
  return newArray
}

from frontend-interview.

Cool-Star avatar Cool-Star commented on July 21, 2024
function disorder(arry) {
  return arry.sort(function(a,b) {
     return Math.random() - Math.random();
  })
}

from frontend-interview.

HECHONG999 avatar HECHONG999 commented on July 21, 2024

function disorder(arry) {
return arry.sort(function(a,b) {
return Math.random() - 0.5;
})
}

from frontend-interview.

coolseaman avatar coolseaman commented on July 21, 2024
const combine = (arr) => {

    let arr1 = [[]],
        arr2 = [];

    for(list of arr) {
        let l = new Set(list);
        for(n of l) {
            for(a of arr1) {
                arr2.push([...a, n]);
            }
        }    
        arr1 = arr2;
        arr2 = [];    
    }

    return arr1;
}

from frontend-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.