js json数组 及 数组的一些操作


<script>
var jsonArr = [
{id: 10, name: 'a'},
{id: 7, name: 'n'},
{id: 1, name: 'x'},
{id: 3, name: 'y'},
{id: 2, name: 'c'},
]

var arr = [1, 2, 1, 2, 34, 5, 23112, 1];

//排序数组
function sortArr(a, b) {
return a - b
}

console.log(arr.sort(sortTest))
//找数组中的最大值
console.log(Math.max.apply(Math, arr))
//找数组中的最大值的索引
console.log(arr.indexOf(Math.max.apply(Math, arr)))

//找出json数组中最大值的索引
var cur_index = 0
for (index in result) {
if (result[cur_index]['id'] < result[index]['id']) {
cur_index = index
}
}
console.log(cur_index)

//排序json数组
function sortJsonArr(a, b) {
return a.id - b.id
}

result.sort(sortJsonArr);
console.log(jsonArr);
</script>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM