作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个听起来像这样的作业:
计算机将两个带有随机字符串的数组放入 Test.data 中。需要一个列表,其中包含 a 中的一个元素,然后 b 中的一个元素,依此类推。
示例:
a: ['a', 'b', 'c']
b: ['d', 'e']
-> ['a', 'd', 'b', 'e', 'c']
我尝试了这段代码,但它只是替换了 Test.data 中的数据。
Test.data = function arry(a, b) {
const c = [];
for (let i = 0; i < Math.max(a.length, b.length); i++) {
if (a[i] != undefined) {
c.push(a[i]);
}
if (b[i] != undefined) {
c.push(b[i]);
}
}
}
错误在于我如何将函数应用于对象,但我不知道如何解决
最佳答案
我认为这就是您想要做的事情?
function arry(a, b) {
const c = [];
for (let i = 0; i < Math.max(a.length, b.length); i++) {
if (a[i] != undefined) {
c.push(a[i]);
}
if (b[i] != undefined) {
c.push(b[i]);
}
}
return c;
}
let alpha = ['a', 'b', 'c'];
let beta = ['d', 'e'];
Test.data = arry(alpha, beta)
关于javascript - 如何将函数应用于对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238507/
我是一名优秀的程序员,十分优秀!