Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

splice插入 #575

Open
smallC-L-Y opened this issue Jul 5, 2022 · 0 comments
Open

splice插入 #575

smallC-L-Y opened this issue Jul 5, 2022 · 0 comments

Comments

@smallC-L-Y
Copy link

smallC-L-Y commented Jul 5, 2022

let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let b = ['A', 'B', 'C', 'D'];
let i = 0;
let m = 0;
for (let index = 0; index < a.length; index++) {
if (index % 2 == 0 && b[i]) {
++m;
// 插入位置的为 奇数 插入数组元素对应的索引 + 1
// i 也可以换成 b.indexOf(b[i]) + 1
a.splice((2 * m - 1) + i + 1, 0, b[i]);
i++;
}
}

@smallC-L-Y smallC-L-Y changed the title 个人想法 splice插入 Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant