JS 数据存储 ES6 rest spread symbol

本地储存 localStorage 没有性能消耗。永远不会消失,只能手动删除。

localStorage 是window对象 window.localStorage

//本地数据存储  
localStorage.setItem('name','zt');

localStorage.setItem('age',50);
//将数据存储到本地中

let name=localStorage.getItem('name');

let age= localStorage.getItem('age');

console.log(name,age);

//更新数据
localStorage.setItem('name','zttt');

localStorage.age='50';

name=localStorage.getItem('name');

age=localStorage.getItem('age');

console.log(name,age);

// localStorage.removeItem('age'); //删除单个
// localStorage.clear();  //删除所有本地数据

const scores = [ //数组对象
  { student: 'summer', score: 90 },
  { student: 'lucy', score: 100 },
  { student: 'summer', score: 60 },
  { student: 'lucy', score: 80 },
  { student: 'summer', score: 80 },
];
localStorage.setItem('scores',JSON.stringify(scores))
console.log(JSON.stringify(scores)); //转换成字符串
const stored=localStorage.getItem('scores');

console.log(JSON.parse(stored)); //  JSON字符串 转换成 字符格式 

//rest参数 ...
const double=(...nums)=>{
  console.log(nums);
  return nums.map((num)=>num*2 )}

const result=double(1,2,3,4,5,6,7); 
console.log(result);//[2, 4, 6, 8, 10, 12, 14]

//spread 语法  数组

const users=['summer','lucy','henty'];
const  members=['lily','tom',...users];
console.log(members)

//spread 语法 对象
const people={name:'john',age:30,job:'teacher'};

const peopleClone={...people,location:'changsha'};
people.age=40;

console.log(peopleClone)

//集合  Set  集合没有length属性 只有 size  属性
//set是ES6中的数据结构,和数组的区别是set不能有重复的值 自动删除过滤重复的值

const  namesArray=['summer','lucy','henry'];
console.log(namesArray);

const namesSet=new Set(['summer','lucy','henry']);
console.log(namesSet);


const items = new Set([1,2,3,4,5,5,5,5,]);
console.log(items.size); //5

//集合转数组 [...namesSet]
const  uniqueNames=[...new Set(namesArray)];

console.log(uniqueNames);

//创建 集合

const ages= new Set();

ages.add(19);
ages.add(15);
ages.add(19);
ages.add(30);
ages.delete(30);
console.log(ages); //Set(2) {19, 15}

const userObject=new Set([
  {name:'lucy',age:30},
  {name:'henry',age:32}
]);
console.log(userObject);
userObject.forEach(
  (user)=>{
    console.log(user.name,user.age)
  }
)

//symbol 原始数据类型  不能被重复
//应用于Object对象里 创建唯一的key值

const  symbolOne=  Symbol('friends');

const  symbolTwo=  Symbol('friends');

console.log(symbolOne===symbolTwo); //false

const summer ={};
summer.age=25;

summer['hobby']='sports';
summer['hobby']='music';

summer[symbolOne]='zt';
summer[symbolTwo]='ztt';
console.log(summer); //{age: 25, hobby: 'music', Symbol(friends): 'zt', Symbol(friends): 'ztt'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值