
1、js 一共有三种创建方法:1)使用内置对象。2)使用JSON符号。3)自定义对象构造 。
2、JavaScript可用的内置对象可分为两种: 1)JavaScript语言原生对象(语言级对象),如String、Object、Functi等。2)JavaScript运行期的宿主对象(环境宿主级对象),如window、document、body等。
上代码
var str = new String("实例初始化String"); var str1 = "直接赋值的String"; var func = new Function("x","alert(x)");//示例初始化func var o = new Object();//示例初始化一个Object
3、使用json符号
var somebooks = { book:[{name:"三国演义"},{name:"西游记"},{name:"水浒传"},{name:"红楼梦"}], author:[{name:"罗贯中"},{name:"吴承恩"},{name:"施耐安",{name:"曹雪芹"}}] }
4、自定义对象构造 创建高级对象构造有两种方式:使用“this”关键字构造、使用原型prototype构造。如:
function Girl() { thisname = "big pig"; thisage = 20; thisstanding; thisbust; thiswaist; thiship; }
json是种常用数据传输格式,js怎么解析json格式的数据呢,请参阅下面的代码:
[html] view plain copy print
<html>
<head>
<script type="text/javascript">
var data = '{"return_code":0,"return_message":"success","data":{"data":[{"id":"1","question":"公主令牌在哪交?"},{"id":"2","question":"公主护使有什么用?"},{"id":"3","question":"角斗场在哪?"},{"id":"4","question":"北部断层在哪?"},{"id":"5","question":"欢乐令有什么用?"},{"id":"6","question":"令牌积分有什么用?"},{"id":"7","question":"南部断层在哪?"},{"id":"8","question":"大妖魔令牌交给谁?"},{"id":"9","question":"神工坊在哪?"},{"id":"10","question":"警戒妖珠有什么用?"}]}}';
function ShowData(){
var obj = eval("("+data+")");
alert("return_code:"+obj["return_code"]);
alert("return_message:"+obj["return_message"]);
alert("第一个问题id:" + obj["data"]["data"][0]["id"]);
alert("第一个内容id:" + obj["data"]["data"][0]["question"]);
}
</script>
</head>
<body onload="ShowData();">
{"return_code":0,"return_message":"success","data":{"data":[{"id":"1","question":"公主令牌在哪交?"},{"id":"2","question":"公主护使有什么用?"},{"id":"3","question":"角斗场在哪?"},{"id":"4","question":"北部断层在哪?"},{"id":"5","question":"欢乐令有什么用?"},{"id":"6","question":"令牌积分有什么用?"},{"id":"7","question":"南部断层在哪?"},{"id":"8","question":"大妖魔令牌交给谁?"},{"id":"9","question":"神工坊在哪?"},{"id":"10","question":"警戒妖珠有什么用?"}]}}
</body>
</html>
const List = [{"end_date":"2022-02-10","end_seconds":16444882950,"end_total_seconds":0,"first_heart_beat_time":"2022-02-10 18:08:34","id":10319,"last_heart_beat_time":"2022-02-10 18:18:15","scanner_id":167,"start_date":"2022-02-10","start_seconds":16444877140,"start_total_seconds":581},{"end_date":"2022-02-11","end_seconds":16445504970,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 11:19:08","id":10338,"last_heart_beat_time":"2022-02-11 11:34:57","scanner_id":167,"start_date":"2022-02-11","start_seconds":16445495480,"start_total_seconds":949},{"end_date":"2022-02-11","end_seconds":16445582200,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 11:48:41","id":10339,"last_heart_beat_time":"2022-02-11 13:43:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":16445513210,"start_total_seconds":6899},{"end_date":"2022-02-11","end_seconds":16445588200,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 13:53:40","id":10343,"last_heart_beat_time":"2022-02-11 13:53:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":16445588200,"start_total_seconds":0},{"end_date":"2022-02-11","end_seconds":16445636200,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 14:03:41","id":10344,"last_heart_beat_time":"2022-02-11 15:13:40","scanner_id":167,"start_date":"2022-02-11","start_seconds":16445594210,"start_total_seconds":4199},{"end_date":"2022-02-11","end_seconds":16445865510,"end_total_seconds":0,"first_heart_beat_time":"2022-02-11 15:55:51","id":10350,"last_heart_beat_time":"2022-02-11 21:35:51","scanner_id":167,"start_date":"2022-02-11","start_seconds":16445661510,"start_total_seconds":20400},{"end_date":"2022-02-13","end_seconds":16447594680,"end_total_seconds":0,"first_heart_beat_time":"2022-02-13 14:47:48","id":10410,"last_heart_beat_time":"2022-02-13 21:37:48","scanner_id":167,"start_date":"2022-02-13","start_seconds":16447348680,"start_total_seconds":24600},{"end_date":"2022-02-14","end_seconds":16448463040,"end_total_seconds":0,"first_heart_beat_time":"2022-02-14 10:15:05","id":10430,"last_heart_beat_time":"2022-02-14 21:45:04","scanner_id":167,"start_date":"2022-02-14","start_seconds":16448049050,"start_total_seconds":41399},{"end_date":"2022-02-15","end_seconds":16449119990,"end_total_seconds":0,"first_heart_beat_time":"2022-02-15 15:30:00","id":10468,"last_heart_beat_time":"2022-02-15 15:59:59","scanner_id":167,"start_date":"2022-02-15","start_seconds":16449102000,"start_total_seconds":1799},{"end_date":"2022-02-15","end_seconds":16449320840,"end_total_seconds":0,"first_heart_beat_time":"2022-02-15 19:44:44","id":10472,"last_heart_beat_time":"2022-02-15 21:34:44","scanner_id":167,"start_date":"2022-02-15","start_seconds":16449254840,"start_total_seconds":6600},{"end_date":"2022-02-16","end_seconds":16450202170,"end_total_seconds":0,"first_heart_beat_time":"2022-02-16 15:13:37","id":10491,"last_heart_beat_time":"2022-02-16 22:03:37","scanner_id":167,"start_date":"2022-02-16","start_seconds":16449956170,"start_total_seconds":24600},{"end_date":"2022-02-17","end_seconds":16450892450,"end_total_seconds":0,"first_heart_beat_time":"2022-02-17 14:59:06","id":10521,"last_heart_beat_time":"2022-02-17 17:14:05","scanner_id":167,"start_date":"2022-02-17","start_seconds":16450811460,"start_total_seconds":8099}]
const newArray = []
ListforEach(item => {
// 第一次循环,repeat定义repet,基于find浅拷贝特性, *** 作repeat会影响到newArray
// 循环开始,find的条件不成立,就会push一个新对象{data: itemstart_date,times: [item]}进去
// 检索条件要注意,find的核对条件需要准确
// 第二次循环,find条件成立,则走else
const repeat = newArrayfind(res => resdata === itemstart_date)
if (!repeat) {
newArraypush({
data: itemstart_date,
times: [item]
})
} else {
// 避免循环紊乱,不用itemtimespush(item),选用repeattimespush(item)
repeattimespush(item)
}
})
打印数据后可以看到,已经将数据成功照相同时间的start_date重新管理
如果前端需要解决时间归类当天的用户多部 *** 作但是后端没处理,就可以用forEach+find的方法处理
以上就是关于如何获取一个js数组的第一个对象全部的内容,包括:如何获取一个js数组的第一个对象、js一个对象数组,如何在不知道KEY的情况下取到值、JS数组对象中有相同值的数据拿出相同的对象重组等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)