我想要给出那些例子的唯一数据数组

时间:2018-10-15 12:06:27

标签: javascript arrays

我有一个数组 喜欢 var data = [1,1,3,4,2,4,5,6,7,5,4] 而且我需要没有循环或任何Lodash和下划线js函数的唯一数据数组

3 个答案:

答案 0 :(得分:2)

使用组合

 var data= [1,1,3,4,2,4,5,6,7,5,4] ;
 const unique = [...new Set(data)];
 console.log(unique); 

答案 1 :(得分:1)

对于ES6 / ES2015:使用Set,单行解决方案是:

private void addLineBetweenHits(HitResult hitResult, Plane plane, MotionEvent motionEvent) {

    int val = motionEvent.getActionMasked();
    float axisVal = motionEvent.getAxisValue(MotionEvent.AXIS_X, motionEvent.getPointerId(motionEvent.getPointerCount() - 1));
    Log.e("Values:", String.valueOf(val) + String.valueOf(axisVal));
    Anchor anchor = hitResult.createAnchor();
    AnchorNode anchorNode = new AnchorNode(anchor);


    if (lastAnchorNode != null) {
        anchorNode.setParent(arFragment.getArSceneView().getScene());
        Vector3 point1, point2;
        point1 = lastAnchorNode.getWorldPosition();
        point2 = anchorNode.getWorldPosition();

    /*
        First, find the vector extending between the two points and define a look rotation
        in terms of this Vector.
    */
        final Vector3 difference = Vector3.subtract(point1, point2);
        final Vector3 directionFromTopToBottom = difference.normalized();
        final Quaternion rotationFromAToB =
                Quaternion.lookRotation(directionFromTopToBottom, Vector3.up());
        MaterialFactory.makeOpaqueWithColor(getApplicationContext(), new Color(0, 255, 244))
                .thenAccept(
                        material -> {
                            /* Then, create a rectangular prism, using ShapeFactory.makeCube() and use the difference vector
                                   to extend to the necessary length.  */
                            ModelRenderable model = ShapeFactory.makeCube(
                                    new Vector3(.01f, .01f, difference.length()),
                                    Vector3.zero(), material);
                            /* Last, set the world rotation of the node to the rotation calculated earlier and set the world position to
                                   the midpoint between the given points . */
                            Node node = new Node();
                            node.setParent(anchorNode);
                            node.setRenderable(model);
                            node.setWorldPosition(Vector3.add(point1, point2).scaled(.5f));
                            node.setWorldRotation(rotationFromAToB);
                        }
                );
        lastAnchorNode = anchorNode;
    }
}

或使用传播运算符

缩短
var data= [1,1,3,4,2,4,5,6,7,5,4] ;
var uniqueItems = Array.from(new Set(data);

答案 2 :(得分:0)

您可以如下使用uniq()中的Underscore.js。这也有sort选项。

import * as _ from 'underscore';

var data= [1,1,3,4,2,4,5,6,7,5,4];
var unique = _.uniq(data);