JavaScript数组剪接 [英] JavaScript array splice

查看:199
本文介绍了JavaScript数组剪接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好计算器社区!

目前,我完全地卡住我的code,已尝试不同的方法采摘时要删除阵列的值,我知道我必须使用拼接它,但由于某种原因,它没有做什么,我想要它做

At the moment I am completly stuck with my code, have try different ways to remove a value from array when picked, I know I have to use splice for it, but for some reason it is not doing what I want it to do

由于网页是相当困难的,因为我的英语AINT说的好,我做它的一些截图来解释,我希望你能得到的想法,当你看到什么,我想做的事情!

Since the web page is quite difficult to explain as my english aint that good, I have made some screenshots of it, I hope you will get the idea when you see what I want to do!

这是用户将看到什么时,他访问网页,它是一个basicly游戏勒诺尔芒。用户不得不拖动3卡在下面的卡的灰色框。

This is what the user will see when he visit the webpage, it's basicly a lenormand game. the user has to drag 3 cards in to the gray box below the cards.

有一次,他已经拖3卡框,用户可以点击图像(卡)把他们(翻转)

Once he has dragged 3 cards to the box, the user can click on the images (Cards) to turn them (flip)

这工作得很好,唯一的问题是,我不希望用户选择相同的卡两次甚至三次,因为我做了数组,我想我应该用拼接的功能,但到目前为止,没有结果!

This works fine, the only problem is that I don't want the user to pick the same cards twice of even three times, Since I do this with a array, I thought I should use Splice function, but so far, no result!

对于JavaScript的code,这里是 http://jsfiddle.net/dkk2nqyg/

for the JavaScript code, here is the JSFiddle http://jsfiddle.net/dkk2nqyg/

这里的某个地方,我必须补充拼接

Somewhere here, I must add the splice

$(function () {
var cars = ["2", "3", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "4"];
var rand = cars[Math.floor(Math.random()*cars.length)];

    $(".cards img").each(function(index) {
        $(this).wrap('<div class="front"></div>')
           .parent().wrap('<div class="flipper"></div>')
           .parent().wrap('<div class="flip-container"></div>')
           .append('<div class="back"><img src="./kaart/'+cars[Math.floor(Math.random()*cars.length)]+'.png"</img> </div>');
});

我已经try'd许多解决方案,但问题是,我需要在这里拼接它,ATLEAST,我想:

I have try'd many solutions, but the problem is that I need to splice it here, atleast, I think:

.append('<div class="back"><img src="./kaart/'+cars[Math.floor(Math.random()*cars.length)]+'.png"</img> </div>');

我会很高兴有这个问题解决了,我希望计算器可以提供我的帮助我想:)

I would gladly have this issue resolved and I hope StackOverflow can offer me the help I want :)

谢谢!

推荐答案

您可以使用拼接()来删除该数组中的项,因为剪接返回一个数组,然后您可以在索引0在访问该元素,以获得牵强的src值

You can use splice() to remove an item from the array, since splice returns an array then you can access the element at the index 0 to get the fetched src value

'<div class="back"><img src="./kaart/' + cars.splice(Math.floor(Math.random() * cars.length),1)[0] + '.png"</img> </div>'

这篇关于JavaScript数组剪接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆