js遍历data&&截取data&&传参格式
程序员文章站
2022-05-25 08:08:02
...
获取数据:遍历data->截取data;不取第一个值->输出data
- 第1种 forEach循环
- 第2种 for循环
//第1种 forEach循环
var data = [];
testData.forEach((item, index, testData) => { // item为arr的元素,index为下标,arr原数组
if(index == 0){
return false;
}
else{
data.push(item)
}
});
console.log(data);
//第2种 for循环
var data1 = [];
for(var i = 0, len = testData.length; i < len; i++) { // 这里的i是代表数组的下标
switch(i){
case 0: break;
default:data1.push(testData[i])
}
}
console.log(data1)
//测试数据
var testData = [
{
"receiverId": "ID",
"quantity": "转出数量"
},
{
"receiverId": 10010,
"quantity": 10
},
{
"receiverId": 10011,
"quantity": 10
},
{
"receiverId": 10012,
"quantity": 10
},
{
"receiverId": 10013,
"quantity": 10
},
{
"receiverId": 10014,
"quantity": 10
},
{
"receiverId": 10015,
"quantity": 10
},
{
"receiverId": 10016,
"quantity": 10
},
{
"receiverId": 10017,
"quantity": 10
},
{
"receiverId": 10018,
"quantity": 10
},
{
"receiverId": 10019,
"quantity": 10
}
];
交互必用:转格式
//ajax传参:转换data格式 转为string
console.log(data);
console.log(typeof(data));//object
//console.log(JSON.parse(data));
var prams = {
'userCounts':data
}
console.log(prams);
console.log(typeof(prams));//object
console.log(JSON.stringify(prams));
prams = JSON.stringify(prams)
console.log(typeof(prams));//string