English 中文(简体)
Javascript Split Array, Split 再次保留Same指数
原标题:Javascript Split Array, Split again & Keep Indexes the Same
  • 时间:2024-03-26 23:42:15
  •  标签:
  • javascript

我已经指出,我试图分裂成一个阵列。

清单上的项目并不总是遵循同样的模式。 这里的例子包括:

TestOne__Test One|1
TestTwo__Test Two|1
TestThree
TestFour__Test Four
TestFive|1

第一,每个项目均按__年分开,这就形成了各部分的阵列,包括第1部分和第2部分(包括第__条的任何线)。

但是,也包含“<>suffix”的任何清单项目也必须分开。

理想的情况是,我最后会看到一阵列(就每个部分而言):

parts = [TestOne, Test One, 1]
parts = [TestTwo, Test Two, 1]
parts = [TestThree,   ,   ]
parts = [TestFour, Test Four,   ]
parts = [TestFive,   , 1]

第一部分:

var promptList = [ TestOne__Test One|1 ,  TestTwo__Test Two|1 ,  TestThree ,  TestFour__Test Four ];
for (var i = 0; i < promptList.length; i++) {
    var parts = promptList[i].split( __ );
}

我曾试图一劳永逸地分裂:

var parts = promptList[i].split(/__||/);

但这只是 array倒了阵列指数。 我想,所有各部分[0]项目都是该项目的第一部分,而第4.1部分是该项目的第二部分(如果有的话),第1部分(如果有)。

每一行的辛迪加如下:

*TestOne* = the data that is too be saved
*Test One* = the display text that is to be shown to the user if this value is exists, otherwise "TestOne" is shown
*1* = whether or not the value will be pre-selected

我愿提及第2部分,并确保其中提及每个项目的1项价值。 在上述清单中,第5行中的第1部分为1数值。

感谢!

问题回答

页: 1 您希望的常规表述是/>>/>>>>(或/__>[>>>>>>> > 如果您想利用特性类别逃避垂直条码,则 2<>>>>>>><>>>><>>>><>>>>>>/>>>>>>(或“垂直条”,而不是“强调”-或外向-或垂直-条。

为了在分裂之后插入空洞,你可以这样做:

const data = `TestOne__Test One|1
TestTwo__Test Two|1
TestThree
TestFour__Test Four
TestFive|1`

const result = data.split( 
 ).map(line => {
  let [a=  , b=  , c=  ] = line.split(/__||/)
  return [a,b,c]
})

console.log(result)

诚然,它不是美丽的,而是行之有效的:

const data = `TestOne__Test One|1
TestTwo__Test Two|1
TestThree
TestFour__Test Four
TestFive|1`

const result = data.split( 
 ).map(line =>
  line.replace(/__|(|.*)?$/,"__$1")
      .replace(/||$/,"__")
      .split("__")
)

console.log(JSON.stringify(result));

第一个<代码>replace()确保始终在><>>/code>上登出。 如果有的话,则在<代码>(>>>>(>>>>>*>>>末或之前插入“t”/code>,如果存在的话(?/code>)。 第二个<代码>replace() 然后在>/code>的末尾添加>,如果已经出现n t。 http://europa-eu-un.org 电话将只能更换大部分ce。 最后的<代码>split()将始终按照欧佩组织的要求归还包含三个内容的阵列。

此处略为改动,如果是第二个<代码>__,则会更宽容。 见

const data = `TestOne__Test One|1
TestTwo__Test Two|1
TestThree
TestFour__Test Four
TestFive|1
Test six|2__3
Test 7__bla__3__4`

const result = data.split( 
 ).map(line =>
  line.replace(/__|(|.*)?$/,"@sep@$1")
      .replace(/||$/,"@sep@")
      .split("@sep@")
)

console.log(JSON.stringify(result))

@sep@是一种任意的示意图,预计从来不会出现在输入指示中(data)。

我认为我找到了解决办法。

var promptList = [ TestOne__Test One|1 ,  TestTwo__Test Two|1 ,  TestThree ,  TestFour__Test Four ,  TestFive|1 ];
for (var i = 0; i < promptList.length; i++) {
  var parts = promptList[i].split(/__|[|]/);
  if (parts[1] ==  1 ) {
     parts[2] =  1 ;
     parts[1] = undefined;
  }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.