Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说ES6 字符串、数组、对象之间的转换[通俗易懂],希望能够帮助你!!!。
下面介绍一个方法
[] : 数组
{}:对象
let obj={
name:"wz",
age:"12",
sex:"女"
}
console.log(Object.values(obj))
let arr=Object.values(obj)
console.log(Object.entries(obj))
let dataList = []
let objectData1 = {
test: '测试',
111:'成功'
}
let objectData2 = {
key1: 'value1',
key2:'value2'
}
dataList.push(objectData1)
dataList.push(objectData2)
console.info("dataList",dataList)
const arr = [
{ key: "id", name: "编号" },
{ key: "name", name: "名称" },
];
const obj = Object.fromEntries(arr.map(item => [item.key, item]));
console.log(obj);
// 输出
// {
// id: { key: "id", name: "编号" },
// name: { key: "name", name: "名称" }
// }
let str1 = 'test'
let srt2 = '111'
let list = [ ]
list.push(str1)
list.push(srt2)
console.info("list",list)
list:['test','111']
let str3 = list.join(',') // 逗号分隔
console.info('str3', str3)
let str1 = 'test'
let srt2 = '111'
let objectData = {
key1: str1,
key2: srt2
}
console.info("objectData",objectData)
let str1 = 'test'
let srt2 = '111'
let objectData = {}
objectData[str1] = '测试'
objectData[srt2] = '成功'
console.info("objectData",objectData)
let objectData = { key1: 'value1', key2:'value2' } objectData.key3 = 'value3' console.info("objectData",objectData)
打印如下
Object.assign
方法的第一个参数是目标对象,后面的参数都是源对象
let test= { a: 1 }; let source1 = { b: 2 }; let source2 = { c: 3 }; Object.assign(test, source1, source2); console.info("test",test)
打印如下
let objectData = { key1: 'value1', key2:'value2', key3:'value3' } delete objectData.key3 console.info("objectData",objectData)
打印如下
// 将key3改为test let objectData = { key1: 'value1', key2:'value2', key3:'value3' } // 先把想要修改属性对应的value拿到 let str = objectData.key3 objectData.test = str // 删除key3 delete objectData.key3 console.info("objectData",objectData)
打印如下
let objectData = { key1: 'value1', key2:'value2', key3:'value3' } Object.keys(objectData).map((key)=>{ console.info("objectData[key]",objectData[key]) // 获取到属性对应的值,做一些处理 })
打印如下
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。