如果我有以下数组
let array = [
{review: 'not_started', status: ''},
{review: 'not_started', status: ''},
{review: 'not_started', status: ''},
{review: 'not_started', status: ''}
]
如果对数组中的所有元素的审核为not_started
,我想将第一个的状态更新为Begin here
,将其他状态更新为已锁定。喜欢这里
let array = [
{review: 'not_started', status: 'Begin here'},
{review: 'not_started', status: 'locked'},
{review: 'not_started', status: 'locked'},
{review: 'not_started', status: 'locked'}
]
当第一个评论为completed
时,请将其状态更新为completed
,将第二个状态更新为Begin here
,将其他状态更新为locked
,如此处
let array = [
{review: 'completed', status: 'completed'},
{review: 'not_started', status: 'Begin here'},
{review: 'not_started', status: 'locked'},
{review: 'not_started', status: 'locked'}
]
如果完成前两个元素审核,则数组将如下所示
let array = [
{review: 'completed', status: 'completed'},
{review: 'completed', status: 'completed'},
{review: 'not_started', status: 'Begin here'},
{review: 'not_started', status: 'locked'}
]
我尝试循环遍历数组(.map
)但无法弄清楚如何根据审核更新状态。
我如何做出反应?
由于
答案 0 :(得分:0)
let array = [
{review: 'not_started', status: ''},
{review: 'not_started', status: ''},
{review: 'not_started', status: ''},
{review: 'not_started', status: ''}
]
let not_started = true;
array.forEach((item)=>{
if(item.review!=='not_started'){
not_started=false;
}
})
if(not_started){
array = array.map((item,index)=>{
if(index==0){
return {review: 'not_started', status: 'Begin here'}
}
else return {review: 'not_started', status: 'locked'}
})
}
/////////////////////second part of the question/////////////////////////////
for(let i=0;i<array.length;i++){
if(array[i].review==='completed'){
array[i].status='completed';
if(array[i+1].review==='completed')
continue;
else{
array[i+1] = {review: 'not_started', status: 'Begin here'};
i++;
}
}
else {
array[i]= {review: 'not_started', status: 'locked'};
}
}
console.log(array)
答案 1 :(得分:0)
let all_reviews_not_started = true
let modified_state = []
array.forEach(item => {
if(item.review !== 'not_started') {
all_reviews_not_started = false
}
})
if(all_reviews_not_started) {
modified_state = array.map((item, i) => {
if(i == 0) return {review: 'not_started', status: 'Begin Here'}
else {review: 'not_started', status: 'locked'}
})
} else {
modified_state = [...array]
array.forEach((item, i) => {
if(item.review == 'completed' && modified_state[i+1].status == 'locked'){
modified_state[i].status = 'completed'
modified_state[i+1] = {review: 'not_started', status: 'Begin here'}
}
}
答案 2 :(得分:0)
let STATUS_BEGIN = 'Begin here';
let STATUS_LOCKED = 'locked';
let STATUS_COMPLETE = 'completed';
let STATUS_NOT_STARTED = 'not_started';
function updateStatus(array) {
var count = 0;
array.forEach(function(e, i) {
if (e.review == STATUS_NOT_STARTED && e.status != STATUS_BEGIN) {
count = count + 1;
}
if (e.review == STATUS_COMPLETE && e.status != STATUS_COMPLETE) {
count = count + 1;
}
if (e.review == STATUS_NOT_STARTED && count == 1) {
e.status = STATUS_BEGIN;
} else if (e.review == STATUS_COMPLETE && count == 1) {
e.status = STATUS_COMPLETE;
if (array[i + 1]) array[i + 1].status = STATUS_BEGIN;
} else if (e.status != STATUS_BEGIN && e.status != STATUS_COMPLETE) {
e.status = STATUS_LOCKED;
}
})
return array;
}
let array = [{
review: STATUS_NOT_STARTED,
status: ''
},
{
review: STATUS_NOT_STARTED,
status: ''
},
{
review: STATUS_NOT_STARTED,
status: ''
},
{
review: STATUS_NOT_STARTED,
status: ''
}
]
updateStatus(array);