JavaScript数组拼接 [英] JavaScript array splice

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

问题描述

Hello StackOverflow社区!

Hello StackOverflow community!

现在,我完全坚持我的代码,尝试不同的方法来删除数组中的值,当挑选,我知道使用splice它,但由于某种原因,它不做我想要它做的

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!

这是用户访问网页时看到的内容基本上是lenormand游戏。用户必须将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)

< img src =https://i.imgur.com/IrrHtlM.pngtitle =source:imgur.com/>

此作品罚款,唯一的问题是,我不想让用户选择相同的卡两次甚至三次,因为我做一个数组,我想我应该使用拼接功能,但到目前为止,没有结果!

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代码,这里是JSFiddle http:/ /jsfiddle.net/dkk2nqyg/

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

在这里,我必须添加splice

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>');
});

我尝试了很多解决方案,但问题是我需要在这里拼接,至少,我认为:

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>');



我会很乐意解决这个问题,我希望StackOverflow可以为我提供帮助:)

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

谢谢!

推荐答案

splice()从数组中删除一个项,因为splice返回一个数组,那么你可以访问索引为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天全站免登陆