首页 > 其他分享 >js 数组方法

js 数组方法

时间:2023-01-03 15:01:55浏览次数:39  
标签:console log 元素 js 数组 fruits const 方法


//定义一个数组
var fruits = ['香蕉', '芒果','橘子','荔枝','樱桃'];


//添加元素到数组的末尾,改变原数组
var newLength = fruits.push('香橙');
//console.log(newLength) //6
//console.log(fruits) //["香蕉", "芒果", "橘子", "荔枝", "樱桃", "香橙"]



//删除数组末尾的元素,改变原数组
var last = fruits.pop()
//console.log(last)//香橙
//console.log(fruits) //["香蕉", "芒果", "橘子", "荔枝", "樱桃"]



//删除数组最前面的元素,改变原数组
var first = fruits.shift();
//console.log(fruits) //["芒果", "橘子", "荔枝", "樱桃"]



//添加元素到数组的头部,改变原数组
var newLength = fruits.unshift('草莓')
//console.log(fruits) // ["草莓", "芒果", "橘子", "荔枝", "樱桃"]



//reverse() 方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。
//改变原数组
console.log(fruits.reverse()) //["樱桃", "荔枝", "橘子", "芒果", "草莓"]



//找出某个元素在数组中的索引
var pos = fruits.indexOf('香蕉');
//console.log(pos) //-1 即不存在这个元素



//通过索引删除某个元素 第一个参数是下标 第二个参数是删除几个,改变原数组
var removedItem = fruits.splice(1, 1)
//console.log(fruits) //["草莓", "橘子", "荔枝", "樱桃"]



//Array.from() 方法从一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。
//非数组解析成一个数组
console.log(Array.from('foo')); // ["f", "o", "o"]



//Array.isArray() 用于确定传递的值是否是一个 Array。
console.log(Array.isArray([1, 2, 3])); //true
//concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3); //["a", "b", "c", "d", "e", "f"]



//join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。
//如果是("")则元素间没有任何字符
const elements = ['火', '土', '水'];
console.log(elements.join()); //"火,土,水"
console.log(elements.join('')); //"火土水"
console.log(elements.join('-'));//"火-土-水"



//slice()方法返回一个由下标截断的数组
//如果只有一个下标,返回包括此下标之后的数组
//如果有两个,第一个下标取,之前的不取,第二个下标不取,之后的不取
//如果第二个下标超出index范围,则第一个数(包括第一个数)后面全部获取
const animals = ['1', '2', '3', '4', '5'];
console.log(animals.slice(2)); //["3", "4", "5"]
console.log(animals.slice(2, 4)); // ["3", "4"]
console.log(animals.slice(1, 5)); // ["2", "3", "4", "5"]



//toString() 返回一个字符串
const array4 = [1, 2, 'a', '1a'];
console.log(array4.toString()); //"1,2,a,1a"



//includes方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
let site = ['runoob', 'google', 'taobao'];
site.includes('runoob'); //true
var fruits = ['香蕉', '芒果','橘子','荔枝','樱桃'];
//使用forEach循环 接收两个参数 数组的值和下标
fruits.forEach(function (item, index) {
// console.log(item, index);
// 香蕉 0
// 芒果 1
// 橘子 2
// 荔枝 3
// 樱桃 4
});



//every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。
const isBelowThreshold =function(currentValue){
return currentValue < 40
};
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold)); //true



//some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。
//它返回的是一个Boolean类型的值
//如果用一个空数组进行测试,在任何情况下它返回的都是false。
const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;
console.log(array.some(even)); //true



//filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
const words = ['啦啦啦啦啦啦啦', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result); //["啦啦啦啦啦啦啦", "exuberant", "destruction", "present"]



//map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
var numbers = [2, 4, 6];
var doubles = numbers.map(function(num,index) {
console.log(index) //0,1,2
return num * 2;
});
console.log(doubles); //[4, 8, 12]

 

标签:console,log,元素,js,数组,fruits,const,方法
From: https://blog.51cto.com/u_12422954/5985785

相关文章

  • js String对象学习
    //charAt()方法从一个字符串中返回指定的字符。letstr='WinterWang'console.log(str.charAt(1))//i//includes()方法用于判断一个字符串是否包含在另一......
  • js 语句
    //try...catch语句标记要尝试的语句块,并指定一个出现异常时抛出的响应。//try如果出现以下情况,catch就抛出一个异常问题所在try{nonExistentFunction();......
  • 字符串 ES6 方法和正则相关方法
    JavaScript内部字符是以UTF-16的格式进行存储;每个字符固定2个字节;对于哪些需要4个字节存储的(unicode码大于0xFFFF的字符),JavaScript会认为它们是2个字符;如上土下口(吉的......
  • 软件方法(下)分析和设计第8章连载[20210518更新]
    墙上挂了根长藤,长藤上面挂铜铃《长藤挂铜铃》;词:元庸,曲:梅翁(姚敏),唱:逸敏,1959您在阅读《软件方法》时如果发现错误,欢迎通过微信umlchina2告知。如果作者认为有道理,决定在下一次......
  • 软件需求设计方法学全程实例剖析幻灯片01-概述[2021-03更新]
    《软件需求设计方法学全程实例剖析》幻灯片pdf文件01-09以及UMLChina模型模板、项目调查表、《软件方法(下)》目前公开内容等资料打包下载地址​​https://pan.baidu.com/s/15......
  • Linux基础知识(13)- GDB 调试器(一)| 安装配置和基本使用方法
    GDB调试器(GNUSymbolicDebugger),是Linux平台下最常用的一款程序调试器。GDB编译器通常以gdb命令的形式在终端(Shell)中使用,它有很多选项。GDB调试器支持C、C++、Go、......
  • MarkDown基本使用方法
    Markdown基本语法使用一级标题二级标题三级标题字体加粗斜体斜体加粗去除线引用引用语法的使用>>分割线图片本地图片网络图片超链接baidu列表......
  • spring项目中添加@Transactional注解后出现NoSuchBeanDefinitionException异常的解决
    https://blog.csdn.net/qq_37059367/article/details/83145779数据库配置文件中修改配置文件里这一句<tx:annotation-driventransaction-manager="transactionManager"/......
  • python + appium 常用公共方法封装
    appium程序下载安装见之前的帖子:https://www.cnblogs.com/gancuimian/p/16536322.htmlappium环境搭建见之前的帖子:https://www.cnblogs.com/gancuimian/p/16557576.html......
  • 《软件方法》8-9章 更新-时刻时段和角色架构型
    第8章分析之分析类图墙上挂了根长藤,长藤上面挂铜铃《长藤挂铜铃》;词:元庸,曲:梅翁(姚敏),唱:逸敏,19598.1 步骤3-1 识别类和属性在业务建模和需求工作流,我们的思考焦点一直在待......