js数组

作者: hszz | 来源:发表于2021-07-24 18:29 被阅读0次

empty表示空位, 它不是一种数据类型,
undefined是一种数据类型, 在数组中表示这个位置的值未定义,
使用forEach()方法遍历时会自动忽略空位, 而使用for循环则会将empty转换为undefined并遍历.

js数组删除

delete arr[i]

被删除的元素变为undefined,数组长度不变,索引不变

var arr = ['a','b','c']
delete arr[1]
console.log(arr)
arr[1]
// 输出结果是 
// (3) ["a", empty, "c"]
// undefined

arr.splice(start,delete_length)

删除的元素键值,数组长度变化,索引变化

var arr = ['a','b','c']
arr.splice(1,1)
console.log(arr)
arr[1]
// 输出结果是
// (2) ["a", "c"]
//  "c"

数组遍历

const todos = [
  {
    id: 1,
    text: 'take out trash',
    isCompleete: false,
  },
  {
    id: 2,
    text: 'dinner with wife',
    isCompleete: false,
  },
  {
    id: 3,
    text: 'Meeting with boss',
    isCompleete: true,
  },
]
// 数组遍历
// for
for (let i = 0; i < todos.length; i++) {
  console.log(`todo ${i + 1}: ${todos[i].text}`)
} 

 // forEach遍历数组   迭代器
todos.forEach(function (todo, i, AllTodos) {
  console.log(`${i + 1}:${todo.text}`)
  console.log(AllTodos)
})

// map()遍历数组,可以返回新数组   迭代器
const todoTextArray = todos.map(function (todo) {
  console.log(todo.text)
  return todo.text
})
console.log(todoTextArray) // 返回有todo.text组成的新数组

// filter()遍历数组,可以根据条件返回数组,  选择器
const todo1 = todos.filter(function (todo) {
  return todo.id === 1;
})
console.log(todo1) 

// 累加器 
// old: 上一次调用回调返回的值,或者是初始值(如例子中的{})
let todosC = todos.reduce(( old, nowItem, index, arr) => {
    return old + nowItem
}, {})

js遍历map

for(var key in jsonData)
    console.log("属性:" + key + ",值:"+ jsonData[key]);
}

[旺柴]


WechatIMG61.jpeg

相关文章

  • js 数组链接concat,和数组转字符串join,字符串转数

    js 数组链接用concat js 数组转成字符串 js 字符串转数组

  • js数组题目

    js面试题 js数组 一、按要求分割数组 将"js,数组,分类"字符串数组以/分割 for循环累加 join()把...

  • js 数组

    js 中数组的长度发生改变,数组就真的发生改变,快速清空数组就让数组长度为0js 数组的方法

  • JS数组以及数组变换

    有关数组 数组对象——一种特殊的对象JS其实没有数组,只使用对象来模拟数组 典型数组和JS数组的区别 典型数组 元...

  • 数组检测

    检测是否是数组: 数组转字符串: 字符串转换数组: js对象转换成js字符串: js字符串转换成js对象:

  • 概念集合

    JS中的数组和Arrary的区别 js中的数组就是array对象

  • JS 数组

    JS 数组是一种特殊的对象,不是真正的数组,它只##是通过对象来模拟数组。 JS 数组的定义 let arr = ...

  • javaScript的数组

    js中没有数组类型 js 中数组是以内置对象的形式存在 数组定义01 var attr = new Array('...

  • 数组

    数组的定义: js:存储多个相同类型数据 ,有序的数据;php数组 1,:索引数组,同js;声明:$arrName...

  • js笔记

    js数组 删除某个元素 js数组是否含有某个元素 判断value为undefined cookie操作

网友评论

      本文标题:js数组

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