标签云

微信群

扫码加入我们

WeChat QR Code

从具体位置[复制]阵列的迁移价值

This question already has an answer here:

I realise there is a lot of topics on this subject but I believe this one is different:

The goal is to get an value from an array on a random location then delete this value.

I use this part by John Resig (the creator of jQuery) to remove an element but it doesn't seem to listen to the location I give it

Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};

this is how I use it

var elements = ['#1','#2','#3','#4']
var R1 = Math.floor(Math.random() * elements.length),
E1 = elements.slice(R1,1)
elements.remove(R1)
var R2 = Math.floor(Math.random() * elements.length),
E2 = elements.slice(R2,1)
elements.remove(R2)
var R3 = Math.floor(Math.random() * elements.length),
E3 = elements.slice(R3,1)
elements.remove(R3)
var R4 = Math.floor(Math.random() * elements.length),
E4 = elements.slice(R4,1)

The problem is the remove function, it doesn't work when removing a object on a specific location I believe.


You didn't write this did you? This is exactly the same implementation that John Resig (the creator of jQuery) wrote.

2018年05月27日24分45秒

it is, i found the first part here sorry for not mentioning it, I'm new here

2018年05月27日24分45秒

What should your code do?

2018年05月28日24分45秒

It should give a random id out of 4 to 4 functions but never one double, so basically i'm tring to get different combination of 1 to 4

2018年05月27日24分45秒

that solves it, thanks!

2018年05月28日24分45秒