比较两个数组值的部分

时间:2018-01-23 11:39:41

标签: javascript

我有两个数组:

Tweener.timeScale

public float shakeMultiplier = 1.0f;
public float shakeTimeScale = 1.0f;

// These values won't be changed
public float baseShakeDuration = 1.0f;
public float baseShakeStrength = 0.1f;
public int baseShakeVibrato = 10;

Vector3 shakePosition;
Tweener shakeTween;

void Start()
{
    shakeTween = DOTween.Shake(() => shakePosition, x => shakePosition = x, baseShakeDuration, baseShakeStrength, baseShakeVibrato, fadeOut: true)
           .SetLoops(-1, LoopType.Restart);
}

void Update()
{
    transform.localPosition = shakePosition * shakeMultiplier;
    shakeTween.timeScale = shakeTimeScale;
}

我想比较上面两个数组,结果将包含sortKey: ["invoiceDate-desc", "invoiceDate-asc", "location-asc", "location-desc", "orderId-asc", "orderId-desc", "invoiceId-asc", "invoiceId-desc", "type-asc", "type-desc", "total-asc", "total-desc"] 中存在的所有值,它们与receivedOrderKey: ["invoiceId", "orderId"] 中的值部分匹配。例如:由于sortKey包含receivedOrderKeyreceivedOrderKey,结果应包含sortKey中的以下值:invoiceId。我目前正在使用两个for循环解决方案来完成这项工作。这样做的有效方法是什么?

for for循环代码:

orderId

1 个答案:

答案 0 :(得分:0)

使用filter

sortKey.filter( s => receivedOrderKey.indexOf( s.replace(/\-(asc|desc)/, "") ) != -1 );

<强>演示

&#13;
&#13;
var sortKey = ["invoiceDate-desc", "invoiceDate-asc", "location-asc", "location-desc", "orderId-asc", "orderId-desc", "invoiceId-asc", "invoiceId-desc", "type-asc", "type-desc", "total-asc", "total-desc"];

var receivedOrderKey = ["invoiceId", "orderId", "status"];



var fnCheck = ( item ) => receivedOrderKey.indexOf( item.replace(/\-(asc|desc)/, "") ) != -1;

var output = sortKey.filter( s => fnCheck(s) );

console.log( output );
&#13;
&#13;
&#13;