<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js根据数组id取值</title>
</head>
<style>
</style>
<body>
<div>
<div id="btn" data-tiao="tiaozhuan">跳转</div>
</div>
<script src="js/jquery-3.2.1.min.js"></script>
<script>
var ary=[{id:1,sortrank:1,name:"空调"},{id:2,sortrank:2,name:"风扇"},{id:3,sortrank:3,name:"油烟机"}];
function getNameById(id) {
for (var i = 0; i < ary.length; i++)
if (ary[i].id == id)
return ary[i];
}
var a = 1;
var b = getNameById(a);
alert(JSON.stringify(b));
</script>
</body>
</html>