Академический Документы
Профессиональный Документы
Культура Документы
Array Helpers
foreach()
Intro
map()
Intro
1/3
Output (in console):
[2, 4, 6, 8]
*/
var cars = [
{model : 'Buick', price: '10k'},
{model : 'Camaro', price: '40k'}
];
var prices = cars.map(function(car) {
return car.price;
});
console.log(prices);
/*
Output (in console):
['10k', '40k']
*/
filter()
var products = [
{name: 'cucumber', type: 'vegetable'},
{name: 'celery', type: 'vegetable'},
{name: 'banana', type: 'fruit'},
{name: 'tomato', type: 'fruit'}
];
var fruits = products.filter(function(product) {
return product.type === 'fruit';
});
console.log(fruits);
/*
Output (in console):
[{"name":"banana","type":"fruit"},
{"name":"tomato","type":"fruit"}]
*/
var products = [
{name: 'cucumber', type: 'vegetable', price: 1 },
2/3
{name: 'celery', type: 'vegetable', price: 3},
{name: 'banana', type: 'fruit', price: 2},
{name: 'tomato', type: 'fruit' price: 2}
];
var affordableVegs = products.filter(function(product) {
return product.type === 'vegetable' && product.price < 3;
});
console.log(affordableVegs);
/*
Output (in console):
[{"name":"banana","type":"fruit","price":1}]
*/
3/3