例子1:把data1数组变成data2类型的数组
data1
:
Array(5)
0
:
{name: "上班", value: "1"}
1
:
{name: "下班", value: "1"}
2
:
{name: "迟到", value: "1"}
3
:
{name: "早退", value: "2"}
4
:
{name: "旷工", value: "2"}
然后要把data1数组形式变成{value:1, 上班(1次)}这种数组
var data = [];
for(var i=0;i<data1.length;i++){
data[i]={value:data1[i].value, name:data1[i].name+"("+data1[i].value+"次)"};
}
例子2:数组去重
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
<script>
var b=[1,1,2,6,4,6,3];
//var b = [‘a‘, ‘b‘, ‘c‘, ‘1‘, 0, ‘c‘, 1, ‘‘, 1, 0];
var arr=[];
for(var i=0;i<b.length;i++){
console.log(b[i]);
console.log("arr1",arr.indexOf(b[i]));
//console.log(indexOf(b[i]));
if(arr.indexOf(b[i])==-1){
arr.push(b[i])
}
console.log("arr2",arr);
//console.log("arr",arr.indexOf(b[i]));
}
console.log(arr);//[1, 2, 6, 4, 3]
</script>
例子3:jq点击按钮进行数组正序倒序排序
<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>;
<script>
$(function(){
var nums = [‘12‘,‘2‘,‘5‘,‘36‘,‘4‘];
$(‘#show8‘).html(nums.join(‘<br/>‘));
$(‘#paixu‘).click(function(){
var ss= $(‘#paixu‘).text();
console.log(ss);
if (ss == "正序") {
$(‘#paixu‘).text("倒序")
nums = nums.sort(function(a,b){
return a-b;
});
$(‘#show8‘).html(nums.join(‘<br/>‘));
}else{
$(‘#paixu‘).text("正序")
nums = nums.sort(function(a,b){
return b-a;
});
$(‘#show8‘).html(nums.join(‘<br/>‘));
};
})
})
</script>
<div id="paixu"><span>正序</span></div>
<h3>排序后</h3>
<div id="show8"></div>
4》js创建四维数组
var tm=[];
for (var i=0;i<24;i++) {
tm[i] = new Array;
for (var j=0;j<60;j++) {
tm[i][j] = new Array;
for (var k=0;k<24;k++) {
tm[i][j][k] = new Array;
for (var p=0;p<60;p++) {
tm[i][j][k].push(p);
}
}
}
}
console.log("tm",tm);
速度超慢,浏览器3秒
5> js造有汉字下标的数据格式
function getLocat(ms){
var dl=ms.data;
var data = [];
//var geoCoordMap = [];
for(var i=0; i<dl.length; i++){
var Nname = dl[i].name;
data.push({name: Nname,value: 61});
//geoCoordMap.push({Nname:[dl[i].longitude,dl[i].latitude]}); Nname变量不识别,
}
var geoCoor= dl;
geoCoordMap = {};
geoCoor.forEach(function(d){
geoCoordMap[d.name] = [d.longitude,d.latitude]
})
console.log("geoCoordMap",geoCoordMap);
console.log("data",data);return;
var data = [
{GPS1: ‘GPS1‘, value: 61},
{GPS2: ‘GPS2‘, value: 61},
];
var geoCoordMap = {
‘GPS1‘:[108.95,34.27],
‘GPS2‘:[108.95,34.27],
};
6>js把数组变为分号隔开得字符串
这两种方法都行
var files = this.data.files;
var strFil = "";
// for (var i = 0; i < files.length;i++){
// console.log(files[i][0]);
// strFil += files[i][0]+";";
// }
console.log(JSON.stringify(files.join(";")));
return;
原文地址:https://www.cnblogs.com/lsc-boke/p/10996905.html