可以使用数组的flat方法将多维数组转换成一维数组,然后使用splice方法将该数组插入到input的任意位置。具体实现如下:

const arr = [1, [2, 3], [4, [5, 6]]];
const flatArr = arr.flat(); // [1, 2, 3, 4, 5, 6]
const input = document.querySelector('input');
const position = 2; // 插入到第二个位置
input.value = input.value.slice(0, position) + flatArr.join(',') + input.value.slice(position);

上述代码中,arr为多维数组,通过调用flat方法将其转换成一维数组flatArr。然后,通过querySelector获取到input元素,并指定插入的位置position。最后,将flatArr转换成字符串,并通过字符串的slice方法将其插入到input的指定位置。

将一个数组平铺出来插入input里任意位置如何实现?

原文地址: https://www.cveoy.top/t/topic/K2g 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录