第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

ReactJS 隨機(jī)映射數(shù)組

ReactJS 隨機(jī)映射數(shù)組

慕運(yùn)維8079593 2023-08-10 15:32:13
我一直在嘗試以隨機(jī)方式映射數(shù)組。我希望數(shù)組的每個(gè)對(duì)象都顯示出來。但刷新時(shí)的順序應(yīng)該始終不同且隨機(jī)。const [ list ] = [        {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false},        {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}    ]; {list.map((item) => (...
查看完整描述

3 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

我認(rèn)為對(duì)數(shù)組進(jìn)行洗牌會(huì)更有用。并且您還應(yīng)該更改常量的定義。


你可以嘗試這樣的事情:


const shuffle = arr => [...arr].sort(() => Math.random() - 0.5);


const list = [

  {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false},

  {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}

];


const newList = shuffle(list);


console.log(newList);


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊

這是一個(gè)具有不可變數(shù)據(jù)范例的純函數(shù):


const shuffleArray = (arr) => {

    // leave arr as it is (immutable data in react)

    const copy = [...arr];

    // our output array

    const output = [];

    // while there are items

    while (copy.length > 0) {

        // removes 1 random element from copy and adds it to output;

        output.push(copy.splice(Math.floor(Math.random() * copy.length), 1));

    }

    // return our random array

    return output;

};


查看完整回答
反對(duì) 回復(fù) 2023-08-10
?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

您應(yīng)該構(gòu)建一個(gè)函數(shù)來隨機(jī)洗牌項(xiàng)目。嘗試下面。


const list = [

        {id: 0, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 1, img: "img", key: 0, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 2, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 3, img: "img", key: 2, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 4, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 5, img: "img", key: 3, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 6, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 7, img: "img", key: 4, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 8, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false},

        {id: 9, img: "img", key: 5, class: "wrap", classFlip: "wrap card-flip", match: false}

    ];

    

const shuffle = array => {

  let currentIndex = array.length,

    temporaryValue,

    randomIndex;


  // While there remain elements to shuffle...

  while (0 !== currentIndex) {

    // Pick a remaining element...

    randomIndex = Math.floor(Math.random() * currentIndex);

    currentIndex -= 1;


    // And swap it with the current element.

    temporaryValue = array[currentIndex];

    array[currentIndex] = array[randomIndex];

    array[randomIndex] = temporaryValue;

  }


  return array;

};


console.log(shuffle(list))


查看完整回答
反對(duì) 回復(fù) 2023-08-10
  • 3 回答
  • 0 關(guān)注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)