美文网首页
js对象数组操作 数组操作

js对象数组操作 数组操作

作者: 蛋壳不讲武德 | 来源:发表于2020-06-09 16:41 被阅读0次

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对象数组操作 数组操作

    var numbers = [1, 2, 3];var newNumbers1 = numbers.map(fun...

  • JS jsonArray操作

    JS jsonArray操作 js对数组对象的操作以及方法的使用 如何声明创建一个数组对象:var arr = n...

  • JS文集的目录

    js基础心法 深浅拷贝(递归)深浅拷贝(首层浅拷贝) js 数据处理 数组对象查找的常见操作数组对象去重的常见操作...

  • JS对象 & JSON & JS数组操作

    JS对象 & JSON & JS数组操作 JSON 格式(JavaScript Object Notation 的...

  • [前端学习]js语法部分学习笔记,第四天

    JS内置数组对象操作 数组名.concat()连接两个或更多的数组 数组名.valueOf() 返回数组本身的值,...

  • JS判断数组

    说明 JS中要区分数组和非数组对象有时候非常的困难,typeof操作符在对数组操作是返回的是 'Object'(除...

  • js数组与对象常用操作方法

    一、Js相关数组操作 数组去除相同的 数组添加数数据 数组反转 打乱数组排序 取数组的前几个 数组扁平化 遍历对象...

  • JavaScript笔记

    JavaScript笔记js的数据类型(6种)js的==和===字符串的操作数组的操作Math对象的操作JSON操...

  • js数组对象或数组常用操作

    找相同 去重 找选中和未选中 js 判断数组的对象中是否有某个值 js如何判断对象数组中是否存在某个对象

  • js对象转数组操作

    在实际开发中,有时候从后台获取到的数据是对象格式,但我们有时只需要知道对象的所有属性值,并存在数组里。 在实际开发...

网友评论

      本文标题:js对象数组操作 数组操作

      本文链接:https://www.haomeiwen.com/subject/djtetktx.html