js对象数组操作 数组操作

var numbers = [1, 2, 3];
var newNumbers1 = numbers.map(function(item) {
return item * 2;
});
console.log(newNumbers1); // 结果:[2,4,6]

===========================================
var cars = [
{
model: "mini",
price: 200
},
{
model: "nio",
price: 300
}
];
var prices = cars.map(function(item) {
return item.price;
});
console.log(prices); //结果:[200, 300]
=============================================
fileter

var products = [
{
name: "cucumber",
type: "vegetable"
},
{
name: "apple",
type: "fruit"
},
{
name: "orange",
type: "fruit"
}
];
var filters = products.filter(function(item) {
return item.type == "fruit";
});
console.log(filters);
//结果:[{name: "apple", type: "fruit"},{name: "orange", type: "fruit"}]

====================================
var products = [
{
name: "cucumber",
type: "vegetable",
quantity: 10,
price: 5
},
{
name: "apple",
type: "fruit",
quantity: 0,
price: 5
},
{
name: "orange",
type: "fruit",
quantity: 1,
price: 2
}
];
var filters = products.filter(function(item) {
//使用&符号将条件链接起来
return item.type === "fruit" && item.quantity > 0 && item.price < 10;
});
console.log(filters);
//结果:[{name: "orange", type: "fruit", quantity: 1, price: 2}]

=================
var post = { id: 1, title: "A" };
var comments = [
{ postId: 3, content: "CCC" },
{ postId: 2, content: "BBB" },
{ postId: 1, content: "AAA" }
];
function commentsPost(post, comments) {
return comments.filter(function(item) {
return item.postId == post.id;
});
}
console.log(commentsPost(post, comments));
//结果:[{postId: 1, content: "AAA"}],返回的是数组


var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.entries();
[0, "Banana"]
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]

你可能感兴趣的:(js对象数组操作 数组操作)