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>
posted @ 2018-03-17 22:54  萧大师  阅读(1734)  评论(0编辑  收藏  举报