<script>
var obj = {
name:"lisi",
age:18
}
var arr = [
{name:"lisi",age:14}
]
// var res = JSON.stringify(obj);
console.log(JSON.stringify(obj))
console.log(JSON.stringify(arr))
var str = `{"name":"李四","age":18}`
var sum =JSON.parse(str)
// json.parse可以将json格式的字符串转换为json对象
console.log(sum)
</script>
示例
<script>
var str = `
[{"name":"李四666","data":[2019,10,11]},
{"name":"张三666","data":[2019,10,11]},
{"name":"王五点击666","data":[2019,10,11]}]
`
/*
var arr = [
{name:"李四...",data:"2019-10-11"}
]
*/
var arr =JSON.parse(str)
var list = [];
arr.forEach(item=>{
var {name,data}=item;
name = name.replace(/\d/g,"")+"...";
data = data.join("-")
list.push({
name,
data
})
})
console.log(list)
//console.log(str.replace(reg,""))
</script>
<script>
var arr =[
{name:"lisi"},
{name:"html"},
{name:"css"},
{name:"vue"},
{name:"vue"},
{name:"html"},
]
// ["lisi","html","css","vue"]
var list = []
arr.forEach(item=>{
var {name} = item;
if(!list.includes(name)){
list.push(name);
}
})
console.log(list)
</script>
<script>
var arr ={
A:[{city:"武汉"},{city:"广州"}],
B:[{city:"深圳"},{city:"广州"}],
C:[{city:"上海"},{city:"武汉"}],
}
var list=[];
for(var key in arr){
console.log(arr[key])
list.push(...arr[key]);
}
//console.log(list)
var res =[];
list.forEach(item=>{
var {city} = item;
console.log(item)
if(!res.includes(city)){
res.push(city);
}
})
console.log(res)
</script>