$ .each对象如何索引 - 1?
因为obj[i - 1].day
无效
var checkin_status = [{
"startdate": "2015-01-08",
"totaldays": "4",
"roadmap": [{
"gifttype": "stars",
"quantity": 100,
"day": 1
}, {
"gifttype": "stars",
"quantity": 500,
"day": 3
}, {
"gifttype": "stars",
"quantity": 1000,
"day": 10
}, {
"gifttype": "stars",
"quantity": 1200,
"day": 20
}, {
"gifttype": "stars",
"quantity": 2200,
"day": 30
}]
}];
var checkin_info = checkin_status[0].roadmap;
$.each(checkin_info, function (i, obj) {
var sum_day = obj[i - 1].day + obj.day;
var middle_day = sum_day / 2;
console.log(middle_day);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
sum_day
输出应为[4,13,30,50]
[2,6.5,15,50]
这是[1,3,10,20,30]
答案 0 :(得分:2)
你根本不需要each
,你将一个数组映射到另一个数组,我将它分成下面的两个操作来演示,但它不需要。这一切都可以在一个map
中完成:
var checkin_status = [{
"startdate": "2015-01-08",
"totaldays": "4",
"roadmap": [{
"gifttype": "stars",
"quantity": 100,
"day": 1
}, {
"gifttype": "stars",
"quantity": 500,
"day": 3
}, {
"gifttype": "stars",
"quantity": 1000,
"day": 10
}, {
"gifttype": "stars",
"quantity": 1200,
"day": 20
}, {
"gifttype": "stars",
"quantity": 2200,
"day": 30
}]
}];
var checkin_info = checkin_status[0].roadmap;
var result1 = checkin_info.slice(1,checkin_info.length).map(function(e,i){
return checkin_info[i].day + e.day;
});
console.log(result1); // logs [4,13,30,50]
var result2 = result1.map(function(e){
return e/2;
});
console.log(result2); // logs [2,6.5,15,50]
&#13;
这当然可以缩短为
var checkin_info = checkin_status[0].roadmap;
var result = checkin_info.slice(1,checkin_info.length).map(function(e,i){
return (checkin_info[i].day + e.day)/2;
});
如果你真的必须拥有$.each
,你可以使用一些相同的原则 - 切掉第一个元素:
$.each(checkin_info.slice(1,checkin_info.length), function (i, obj) {
var sum_day = checkin_info[i].day + obj.day;
var middle_day = sum_day / 2;
console.log(middle_day);
});
答案 1 :(得分:0)
特殊情况第一个条目,如下:
var checkin_status = [{
"startdate": "2015-01-08",
"totaldays": "4",
"roadmap": [{
"gifttype": "stars",
"quantity": 100,
"day": 1
}, {
"gifttype": "stars",
"quantity": 500,
"day": 3
}, {
"gifttype": "stars",
"quantity": 1000,
"day": 10
}, {
"gifttype": "stars",
"quantity": 1200,
"day": 20
}, {
"gifttype": "stars",
"quantity": 2200,
"day": 30
}]
}];
var checkin_info = checkin_status[0].roadmap;
$.each(checkin_info, function (i, obj)
{
var sum_day = 0;
if(i==0)
{
//this is the first entry, so we can't look back to the previous day.
sum_day = obj.day;
}
else
{
//you were calling obj[i-1] here, which wasn't going to work.
//i presume you wanted the previous day here.
sum_day = checkin_info[i - 1].day + obj.day;
}
var middle_day = sum_day / 2;
console.log(middle_day);
});
答案 2 :(得分:0)
$。每个都不利于性能,因为每个交互都会调用函数。最好在vanilla js中使用for循环。
var checkin_status = [
{"startdate":"2015-01-08",
"totaldays":"4",
"roadmap":[
{"gifttype":"stars","quantity":100,"day":1},
{"gifttype":"stars","quantity":500,"day":3},
{"gifttype":"stars","quantity":1000,"day":10},
{"gifttype":"stars","quantity":1200,"day":20},
{"gifttype":"stars","quantity":2200,"day":30},
]
}];
var checkin_info = checkin_status[0].roadmap,
len = checkin_info.length,
i,
sum_day = [],
middle_day = [],
sum = 0;
for(i=0; i< (len -1); i++) {
sum = checkin_info[i].day + checkin_info[i + 1].day;
sum_day.push(sum);
middle_day.push(sum/2);
}
console.log(sum_day)
console.log(middle_day)