如何使用UnderscoreJS进行条件筛选

时间:2016-02-18 14:01:59

标签: javascript angularjs underscore.js

我只想根据条件提取数组元素。

我尝试了_.filter方法,例如_.wherevar carsArray = [{ "name": "Record1", "bookings": [{ "price": 50, "actualPrice": 70, }] }, { "name": "Record2", "bookings": [{ "price": 60, "actualPrice": 100, }] }, { "name": "Record3", "bookings": [{ "price": 100, "actualPrice": 110, }] }]; var availableCars = _.filter(carsArray, function (items) { return _.filter(items, function (cars) { return(cars.price <= 80); }); }); 方法,但我无法获得所需的输出。

为了更清晰, 我不想要一个价格> 1的汽车对象。 80。

我尝试使用下面的代码并获得一个空数组:

001("Pasta"),
002("Pizza"),
003("Nuddle"),
004("Steak"),
005("Rice")

5 个答案:

答案 0 :(得分:1)

适用于bookings数组

中的多个元素

如果bookings数组中有多个元素,并检查元素的任何是否满足,则可以使用条件Array#some

carsArray.filter(car => car.bookings.some(booking => booking.price <= 80));

要检查全部,是否可以使用汽车预订价格低于80 Array#every

carsArray.filter(car => car.bookings.every(booking => booking.price <= 80));

现场演示:

&#13;
&#13;
var carsArray = [{
    "name": "Record1",
    "bookings": [{
        "price": 50,
        "actualPrice": 70,
    }, {
        "price": 40,
        "actualPrice": 70
    }]
}, {
    "name": "Record2",
    "bookings": [{
        "price": 60,
        "actualPrice": 100,
    }, {
        "price": 90,
        "actualPrice": 160
    }]
}, {
    "name": "Record3",
    "bookings": [{
        "price": 100,
        "actualPrice": 110,
    }, {
        "price": 120,
        "actualPrice": 200
    }]
}];

var atLeastOne = carsArray.filter(car => car.bookings.some(booking => booking.price <= 80));
var allCars = carsArray.filter(car => car.bookings.every(booking => booking.price <= 80));

document.getElementById('some').innerHTML = JSON.stringify(atLeastOne, 0, 4);
document.getElementById('every').innerHTML = JSON.stringify(allCars, 0, 4);
&#13;
<strong>For any of the car bookings less than or equal to 80</strong>
<pre id="some"></pre>

<hr />
<strong>For all of the car bookings less than or equal to 80</strong>
<pre id="every"></pre>
&#13;
&#13;
&#13;

适用于bookings数组

中的单个元素

您可以将JavaScript Array#filterArrow function一起使用,如下所示。

carsArray.filter(car => car.bookings[0].price <= 80);

&#13;
&#13;
var carsArray = [{
    "name": "Record1",
    "bookings": [{
        "price": 50,
        "actualPrice": 70,
    }]
}, {
    "name": "Record2",
    "bookings": [{
        "price": 60,
        "actualPrice": 100,
    }]
}, {
    "name": "Record3",
    "bookings": [{
        "price": 100,
        "actualPrice": 110,
    }]
}];

var filteredArr = carsArray.filter(car => car.bookings[0].price <= 80);

console.log(filteredArr);
document.getElementById('result').innerHTML = JSON.stringify(filteredArr, 0, 4);
&#13;
<pre id="result"></pre>
&#13;
&#13;
&#13;

如果bookings数组中只有一个元素,则数据格式可以更改为

var carsArray = [{
    "name": "Record1",
    "bookings": {
        "price": 50,
        "actualPrice": 70,
    }
}, {
    "name": "Record2",
    "bookings": {
        "price": 60,
        "actualPrice": 100,
    }
}, {
    "name": "Record3",
    "bookings": {
        "price": 100,
        "actualPrice": 110,
    }
}];

var filteredArr = carsArray.filter(car => car.bookings.price <= 80);

答案 1 :(得分:1)

这是如何使用underscorejs

[CmdletBinding()]
Param(
    # Name of VPN connection
    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=1)]
    [string]$vpn_name,

    # Server URL or IP
    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=2)]
    [string]$vpn_server,

    # 192.168.72.0/24
    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=3)]
    [string[]]$target_nets
)

以及如何在es6中执行此操作

var availableCars = _.filter(carsArray, function(car) {
    return _.any(car.bookings, function(booking) {
        return booking.price <= 80; 
    });
});

当然,如果车内有多个预订,并且至少有1个预订是&lt; = 80 buckeronies

答案 2 :(得分:1)

我认为你想要的是:

var availableCars = _.filter(carsArray, function(car){
  return _.every(car.bookings, function(booking) {
    return booking.price <= 80;
  });
});

我假设每辆车都有多个预订,而且如果预订的车辆都不超过80辆,那么您只想

答案 3 :(得分:1)

以为我会选择更接近要求的东西

我不想要价格为&gt的汽车对象。 80

function expensiveBooking(booking){
    return booking.price > 80;
}

function carHasExpensiveBooking(car){
    return _.some(car.bookings, expensiveBooking);
}

var result = _.reject(carsArray, carHasExpensiveBooking);

答案 4 :(得分:0)

试试这个:

var availableCars = _.filter(carsArray, function(item){
    return (item.bookings.price <= 80);
    });
});