2
var data = [
    {
        id:1,
        account_name:'Akshay Patil',
        debit:111,
        credit:''
    },
    {
        id:2,
        account_name:'Bharat Chavan',
        debit:222,
        credit:''
    },
    {
        id:3,
        account_name:'Chetan Kore',
        debit:333,
        credit:''
    },
    {
        id:4,
        account_name:'Dilip Patil',
        debit:444,
        credit:''
    },
    {
        id:5,
        account_name:'Eshawr Dange',
        debit:555,
        credit:''
    },
    {
        id:6,
        account_name:'farhan Khan',
        credit:666,
        debit:''
    },
    {
        id:7,
        account_name:'Ganesh Shine',
        credit:777,
        debit:''
    },
    {
        id:8,
        account_name:'Hemant Birje',
        credit:888,
        debit:''
    }
]

problem happen when object remove from array after random number are generated that time is random number is greater than array of length then error like Cannot read property.

This is my main.s file

function(){
 user.wrapper.find('.randomMove').off('click').on('click', function(event){
        var dice = {
           sides: 6,
           roll: function () {
            var randomNumber = Math.floor(Math.random() * this.sides) + 1;
                return randomNumber;
           }
        }
        var result = dice.roll();
        result--;
        //setDataInTable(result);        
        user.data.splice(result,1);
   });
  }

using this js i m splice the generated random object and set to table data

peak
  • 105,803
  • 17
  • 152
  • 177
suraj
  • 79
  • 4
  • I guess your actual problem is to shuffle the array, right? See https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – georg Aug 08 '17 at 11:07
  • **Can you please attach the code that you wrote as well? Thanks.** –  Aug 08 '17 at 11:09
  • function setListeners(){ user.wrapper.find('.randomMove').off('click').on('click', function(event){ var dice = { sides: 6, roll: function () { var randomNumber = Math.floor(Math.random() * this.sides) + 1; return randomNumber; } } var result = dice.roll(); result--; setDataInTable(result); user.data.splice(result,1); }); } – suraj Aug 08 '17 at 11:30
  • @penguin_k plz check my attached code – suraj Aug 08 '17 at 11:56

5 Answers5

0

This should work

var len = data.length;
var rand = Math.random() * (len - 0) + 0;

var selected = data[rand];
data.splice(rand, 1)
dorintufar
  • 660
  • 9
  • 22
0

You should generate the random number according to the array length.

var randomIndex = Math.floor( Math.random() * data.length );
// Index 0 - first
// Index (data.length-1) - last

Now to remove the random element:

var element = data.splice(randomIndex, 1)[0];
console.log(element);

After that, to get another random element, you should generate a new randomIndex and splice again.

boxmein
  • 865
  • 7
  • 19
  • thanks, but i don't want according to array length . i have Dice roller according to this i want solution – suraj Aug 08 '17 at 11:13
0

First you need to find the index of the element to remove:

var array = [1, 10, 100];
//edited to choose it random
var selectedItem = array[Math.floor(Math.random()*array.length)];
var index = selectedItem;

After use splice to remove it:

if (index > -1) {
    array.splice(index, 1);
}
0

I'm assuming this is JavaScript and that the error only occurs after an element has been removed from the array. I'm guessing that you are running through the list and trying to shuffle the contents.

What's happening is this: You start with an array that is 8 elements long. Then you delete one, making it 7 elements long. Then, if it selects element 8 to delete, it can't because there are only 7 elements left. This is how you are getting the error messages.

Use this code instead:

var data = [
    {
        id:1,
        account_name:'Akshay Patil',
        debit:111,
        credit:''
    },
    {
        id:2,
        account_name:'Bharat Chavan',
        debit:222,
        credit:''
    },
    {
        id:3,
        account_name:'Chetan Kore',
        debit:333,
        credit:''
    },
    {
        id:4,
        account_name:'Dilip Patil',
        debit:444,
        credit:''
    },
    {
        id:5,
        account_name:'Eshawr Dange',
        debit:555,
        credit:''
    },
    {
        id:6,
        account_name:'farhan Khan',
        credit:666,
        debit:''
    },
    {
        id:7,
        account_name:'Ganesh Shine',
        credit:777,
        debit:''
    },
    {
        id:8,
        account_name:'Hemant Birje',
        credit:888,
        debit:''
    }
]

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function deleteRandom() {
    data.splice(getRandomInt(0, data.length), 1);  
}

console.log(data);
deleteRandom();
console.log(data);

This code takes your array, finds out how long it is, then removes an element that it knows is in it. For the purpose of demonstration, this one logs the array before and after to the console.

0

var data = [
    {
        id:1,
        account_name:'Akshay Patil',
    },
    {
        id:2,
        account_name:'Bharat Chavan',
    },
    {
        id:3,
        account_name:'Chetan Kore',
    },
    {
        id:4,
        account_name:'Dilip Patil',
    }
]

function removeItem(){
debugger;
    var lengthOfArray = data.length;
    var spilceId = Math.floor(Math.random() * lengthOfArray);
    
    if(lengthOfArray > 0){
      data.splice(spilceId,1)
    }
    // or remove data based on index of id
    console.log(data);
}
<button onclick='removeItem()'>remove</button>
Prabhakaran
  • 1,524
  • 2
  • 13
  • 21