如何在现有数组中添加元素

huangapple go评论86阅读模式
英文:

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 &lt; processMinutes.length; i += 2) {
    if (!processMinutes[i + 1]?.endsWith(&#39;min&#39;))
        processMinutes.splice(i + 1, 0, &#39;0 min&#39;);
}

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 = [&#39;1990&#39;,&#39;180min&#39;,&#39;1991&#39;,&#39;135 min&#39;,&#39;1992&#39;,&#39;126 min&#39;,&#39;1993&#39;,&#39;1994&#39;,&#39;182 min&#39;,&#39;1995&#39;,&#39;1996&#39;,&#39;118 min&#39;,&#39;1997&#39;,&#39;1998&#39;,&#39;136min&#39;,&#39;199&#39;,&#39;144 min&#39;,&#39;2000&#39;]

console.log(processMinutes.reduce((a,c,i,r)=&gt;(a.push(c),
  a.length%2===1&amp;&amp;!(r[i+1]??&#39;&#39;).endsWith(&#39;min&#39;)&amp;&amp;a.push(&#39;0 min&#39;),a),[]))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月3日 17:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定