Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript中的现有数组重新创建数组_Javascript_Arrays - Fatal编程技术网

从Javascript中的现有数组重新创建数组

从Javascript中的现有数组重新创建数组,javascript,arrays,Javascript,Arrays,我有一个javascript字符串数组。我想加入元素并创建一个字符串。现在,在一个特定的长度上,我想要分割字符串(比如说分成3部分),并创建一个包含3个元素的新数组 firstArray = [ 'Hello, this is line one of the array sentence', 'Hello, this is line two of the array sentance' ]; // Output - secondArray = ["He

我有一个javascript字符串数组。我想加入元素并创建一个字符串。现在,在一个特定的长度上,我想要分割字符串(比如说分成3部分),并创建一个包含3个元素的新数组

firstArray = [
        'Hello, this is line one of the array sentence',
        'Hello, this is line two of the array sentance'
      ];
// Output - secondArray = ["Hello, this is line one of"," the array sentence Hello, this is","line two of the array sentance"]
您可以使用匹配功能:

var firstArray=[
“嗨,这是一条很长的线,那”,
“表示每10或15个字符分解一次”
];
//首先加入第一个数组
var joinedaray=firstArray.join(“”);
//分成10块
var tenChunksArray=joinedArray.match(/.{1,10}/g);
console.log(tenChunksArray);
//分成15块
var fifteenChunksArray=joinedArray.match(/.{1,15}/g);

控制台日志(fiftentenchunksarray)您需要将数组合并为一个数组,然后将其分成数字和块。例如

const inputArr = [
  'Hello, this is line one of the array sentence',
  'Hello, this is line one of the array sentence',
];

const resplitArray = (arr, chunkSize) => {
  const res = [];
  const charsArr = arr.join(' ').split('');
  for (let i = 0, j = charsArr.length; i < j; i += chunkSize) {
    res.push(charsArr.slice(i, i + chunkSize).join(''));
  }
  return res;
};

console.log(resplitArray(inputArr, 10));
const inputArr=[
“你好,这是数组句子的第一行”,
“你好,这是数组句子的第一行”,
];
常量响应=(arr,chunkSize)=>{
常数res=[];
const charsArr=arr.join(“”).split(“”);
for(设i=0,j=charsArr.length;i

更新:我更喜欢带有regex match的变体

那么将其分成3部分的规则是什么?这里的输出是您想要的还是您当前得到的?您希望以多大的字符串长度拆分字符串?正如@epascarello所问的,规则是什么?输出应该遵循的模式?如果您只需要针对示例中的情况进行转换,您可以硬编码一些转换,但是如果您想动态地进行转换,则需要一个规则。那会是什么?op已经指定了“在一定长度上”,所以我猜这将是规则。这是否回答了你的问题?