英文:
How to add elements in existing array
问题
[
'1990', '180min', '1991',
'135 min', '1992', '126 min',
'1993', '0 min', '1994',
'182 min', '1995', '0 min',
'1996', '118 min', '1997',
'0 min', '1998', '136min',
'199', '144 min', '2000',
'0 min'
]
英文:
I have an array that shows the amount of service worked minutes for each year.
But for some years, there are no service worked minutes recorded.
In these cases, I need to add "0 min".
Input:
const processMinutes = [
'1990', '180min', '1991',
'135 min', '1992', '126 min',
'1993', '1994', '182 min',
'1995', '1996', '118 min',
'1997', '1998', '136min',
'199', '144 min', '2000'
]
Output:
[
'1990', '180min', '1991',
'135 min', '1992', '126 min',
'1993', '0 min', '1994',
'182 min', '1995', '0 min',
'1996', '118 min', '1997',
'0 min', '1998', '136min',
'199', '144 min', '2000',
'0 min'
]
I have attempted to achieve a result, but have been unsuccessful each time I have tried.
In my opinion, I should utilize the splice array method.
const processMinutes = [
'1990', '180min', '1991',
'135 min', '1992', '126 min',
'1993', '1994', '182 min',
'1995', '1996', '118 min',
'1997', '1998', '136min',
'199', '144 min', '2000'
]
processMinutes.splice(7, 0, "0 min");
processMinutes.splice(11, 0, "0 min");
processMinutes.splice(15, 0, "0 min");
processMinutes.splice(21, 0, "0 min");
console.log(processMinutes)
答案1
得分: 2
你可以迭代并预测下一个项目,然后添加一个零时间,没有时间。
const processMinutes = [
'1990', '180min', '1991',
'135 min', '1992', '126 min',
'1993', '1994', '182 min',
'1995', '1996', '118 min',
'1997', '1998', '136min',
'199', '144 min', '2000'
];
for (let i = 0; i < processMinutes.length; i += 2) {
if (!processMinutes[i + 1]?.endsWith('min'))
processMinutes.splice(i + 1, 0, '0 min');
}
console.log(...processMinutes);
不包含注释的代码已翻译为中文。
英文:
You could iterate and look ahead to the next item and add one zero time, it no time.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const processMinutes = [
'1990', '180min', '1991',
'135 min', '1992', '126 min',
'1993', '1994', '182 min',
'1995', '1996', '118 min',
'1997', '1998', '136min',
'199', '144 min', '2000'
];
for (let i = 0; i < processMinutes.length; i += 2) {
if (!processMinutes[i + 1]?.endsWith('min'))
processMinutes.splice(i + 1, 0, '0 min');
}
console.log(...processMinutes);
<!-- end snippet -->
答案2
得分: 1
在将每一年推入结果后,检查下一个将要推入的项目是否以'min'结尾。如果不是,继续将'0 min'推入结果,然后继续处理源数组中的下一个项目。
英文:
After you push every year into the result, check if the next item that will be pushed will end with 'min'. If not, push '0 min' before continuing to the next item in the source array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const processMinutes = ['1990','180min','1991','135 min','1992','126 min','1993','1994','182 min','1995','1996','118 min','1997','1998','136min','199','144 min','2000']
console.log(processMinutes.reduce((a,c,i,r)=>(a.push(c),
a.length%2===1&&!(r[i+1]??'').endsWith('min')&&a.push('0 min'),a),[]))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论