简体   繁体   English

JavaScript过滤方法-仅返回第一个对象

[英]JavaScript Filter Method - Return only first object

Using the filter() method in JavaScript, is it possible to return only the first instance that passes the test? 使用JavaScript中的filter()方法,是否可以仅返回通过测试的第一个实例? Once I find first object in my cars array, I'd like to move this object from its current position to the beginning of the cars array. 一旦我在cars数组中找到第一个对象,就想将该对象从其当前位置移动到cars数组的开始。

Code: 码:

     cars.filter(function(car) {

        var fareType = car.data("fareType");
        var partnerCode = car.data("partnerCode");

        if (fareType == "DEAL" && partnerCode == "AV")
        {
            // get first instance only and move element to beginning of array
        }

        if (fareType == "DEAL" && partnerCode == "BU")
        {
            // also get first instance where partnerCode == BU only and move element to beginning of array 
           // If dataPrice is less than dataPrice for partner AV, move to first position, else move to second position
        }
    });

I would use .findIndex and .splice : 我会用.findIndex.splice

let index = cars.findIndex(function(car) {
    var fareType = car.data("fareType");
    var partnerCode = car.data("partnerCode");
    return (fareType == "DEAL" && partnerCode == "AV");
});
if (index !== -1) {
    let car = cars[index];
    cars.splice(index, 1);
    cars.unshift(car);
    /*
     * this could be reduced to:
     *
     *     cars.unshift(...cars.splice(index, 1));
     *
     * at the sake of clarity
     */
}

index = cars.findIndex(function(car) {
    return fareType == "DEAL" && partnerCode == "BU";
});
if (index !== -1) {
    let car = cars[index];
    cars.splice(index, 1);
    if (car.data('price') < cars[0].data('price')) {
        cars.unshift(car);
    } else {
        cars.splice(1, 0, car);
    }
}

You can use findIndex() 您可以使用findIndex()

var selectedIndex = cars.findIndex(function(car) {
    return car.data("fareType")== "DEAL" && car.data("partnerCode")== "AV";
});

if (selectedIndex > -1) {
   cars = [cars[selectedIndex], ...cars.slice(0, selectedIndex), ...cars.slice(selectedIndex + 1)
}

You could do something like: 您可以执行以下操作:

let indexToGoFirst = 0;

cars.some((car, index) => {
  const fareType = car.data('fareType');
  const partnerCode = car.data('partnerCode');

  if (fareType == 'DEAL' && partnerCode == 'AV') {
    indexToGoFirst = index;
    return True
  }
})

cars[0] = cars[indexToGoFirst];

The .some() checks if anything in the array matches the condition, and will stop looping when the condition is met, so you can use that to set the first element. .some()检查数组中是否有任何条件与条件匹配,并且在满足条件时将停止循环,因此您可以使用它来设置第一个元素。

Find index and create new array with element first 查找索引并首先使用元素创建新数组

var index = cars.findIndex(function(car) {
        var fareType = car.data("fareType");
        var partnerCode = car.data("partnerCode");

        // get first instance only and move element to beginning of array

        return fareType == "DEAL" && partnerCode == "AV";

    });

var newCars = index > 0? [cars[index]].concat(cars.slice(0, index)).concat(cars.slice(index + 1): cars; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM