我有以下示例数组
let myArray = [
{name: "Sam", status: 'not_started', progress: 'start'},
{name: "Sara", status: 'not_started', progress: 'start'},
{name: "John", status: 'not_started', progress: 'start'},
{name: "Eric", status: 'not_started', progress: 'start'}
]
如果所有元素都具有状态:' not_started'然后我想更新第一个进展到开始'其余的就是“锁定”。所以数组看起来像这样:
let myArray = [
{name: "Sam", status: 'not_started', progress: 'start'},
{name: "Sara", status: 'not_started', progress: 'lock'},
{name: "John", status: 'not_started', progress: 'lock'},
{name: "Eric", status: 'not_started', progress: 'lock'}
]
如果第一个元素的状态已经完成'然后第二个将开始'和休息将会锁定'如下所示
let myArray = [
{name: "Sam", status: 'completed', progress: 'completed'},
{name: "Sara", status: 'not_started', progress: 'start'},
{name: "John", status: 'not_started', progress: 'lock'},
{name: "Eric", status: 'not_started', progress: 'lock'}
]
此外,如果前两个已完成'然后它看起来像下面
let myArray = [
{name: "Sam", status: 'completed', progress: 'completed'},
{name: "Sara", status: 'completed', progress: 'completed'},
{name: "John", status: 'not_started', progress: 'start'},
{name: "Eric", status: 'not_started', progress: 'lock'}
]
我试图通过myArray map
并创建一系列状态和进度值,并根据状态progress: array[status]
获取进度值,但这不会更新进度的进度:'锁定&# 39;正确的,因为' not_started'可以有进步:'开始'或者'锁定'
显然,我需要根据以前的值来更新下一个值但是没有得到它。
有谁知道我怎么能实现这个目标?
由于
答案 0 :(得分:1)
您可以使用map()
方法,在首次出现not_started
状态时,您可以将this.started
设置为true,并根据要更改的进度锁定。
let myArray = [
{name: "Sam", status: 'completed', progress: 'start'},
{name: "Sara", status: 'not_started', progress: 'start'},
{name: "John", status: 'not_started', progress: 'start'},
{name: "Eric", status: 'not_started', progress: 'start'}
]
const result = myArray.map(function(e) {
if(e.status == 'completed') return {...e, progress: 'completed'}
if (this.started) return { ...e, progress: 'lock'}
if (e.status == 'not_started') this.started = true;
return { ...e};
}, {});
console.log(result)
答案 1 :(得分:0)
这就是你需要的吗? 我希望我能正确理解你 https://jsfiddle.net/txn1cb2z/
function iterate(arr) {
let started_key = -1;
for( let index in arr){
if(arr[index].status === 'completed') {
arr[index].progress = 'completed';
continue;
}
if(arr[index].status === 'not_started'){
arr[index].progress = 'start';
started_key = parseInt(index);
break;
}
}
if(started_key < 0 || started_key >= arr.length -1)
return;
for (let i = started_key+1; i < arr.length; i++){
arr[i].progress = 'lock';
}
return started_key;
}
function log(arr) {
for (var index in arr) {
console.log(index,arr[index].status,arr[index].progress);
}
console.log('------------');
}
let myArray = [
{name: "Sam", status: 'not_started', progress: 'start'},
{name: "Sara", status: 'not_started', progress: 'start'},
{name: "John", status: 'not_started', progress: 'start'},
{name: "Eric", status: 'not_started', progress: 'start'}
];
iterate(myArray);
log(myArray);
myArray[0].status = 'completed';
iterate(myArray);
log(myArray);
myArray[1].status = 'completed';
iterate(myArray);
log(myArray);
myArray[2].status = 'completed';
iterate(myArray);
log(myArray);
myArray[3].status = 'completed';
iterate(myArray);
log(myArray);
答案 2 :(得分:0)
您的要求没有任何意义,因为第1步是:
如果所有元素都具有状态:&#39; not_started&#39;然后我想更新 第一个进展到开始&#39;其余的就是“锁定”。所以阵列 看起来像这样:
请注意,项目的所有状态字段仍然是“未启动”。所以调用一个函数来启动下一个仍然只是设置所有进度来锁定&#39;除了第一个。
如果步骤1为:所有项目的状态均为&#34; not_started&#34;第二项有&#34;锁定&#34;那么你可以按照你的描述实际实现其余部分。
const myArray = [
{name: "Sam", status: 'not_started', progress: 'start'},
{name: "Sara", status: 'not_started', progress: 'start'},
{name: "John", status: 'not_started', progress: 'start'},
{name: "Eric", status: 'not_started', progress: 'start'}
]
const isAllNotStarted = arr =>
arr.reduce((allNotStarted,item)=>(item.status==="not_started"&&allNotStarted),true);
const startNext = arr => {
var status = "";
if(isAllNotStarted(arr)&&arr[1].progress!=="lock"){
arr = arr.map(item=>({...item,progress:"lock"}));
arr[0].progress="start";
status = "continue";
}
return arr
.reduce(
([result,status],current)=>{
if(current.progress==="start" && current.status==="not_started" && status===""){
return [result.concat({...current,progress:"completed",status:"completed"}),"startNext"];
}
if(current.progress==="lock" && current.status==="not_started" && status==="startNext"){
return [result.concat({...current,progress:"start"}),"continue"];
}
return [result.concat(current),status];
},
[[],status]
)[0]
}
["first","next","next","start last","complete last","run over (do nothing but copy the array)"]
.reduce(
(arr,item)=>{
console.log(item);
const next = startNext(arr);
console.log(next);
return next;
},
myArray
);
&#13;