gpt4 book ai didi

javascript - 如何将函数应用于对象

转载 作者:行者123 更新时间:2023-11-28 14:46:27 25 4
gpt4 key购买 nike

我有一个听起来像这样的作业:

计算机将两个带有随机字符串的数组放入 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com