I have data two sets of data as follows: "One.Two.Three.Four" "One.Two.Three.1.Four"
我有两组数据如下:“一、二、三。”四个One.Two.Three.1.Four”
The first three parts are fixed and the remaining can extend to as many as possible. I am trying to build an object where I want to split and combine whatever is present after three into an object.
前三部分是固定的,其余部分可以扩展到尽可能多的地方。我正在尝试构建一个对象,在这个对象中,我想要拆分和合并任何在三个之后出现的对象。
var split = samplestr.split('.');
var finalarray = [];
if(split.length>4)
{
finalarray[0] = split[0];
finalarray[1] = split[1];
finalarray[2] = split[2];
finalarray[3] = split[3]+"."split[4];
}
I need to generalise this such that even if the string is of the form
我需要推广这样的东西,即使字符串是形式。
"One.Two.Three.1.2.3.Four"
finalarray[3] = 1.2.3.Four;
Any hints on generalising this?
有什么建议吗?
2 个解决方案
#1
2
With Array#shift
and Array#join
.
与数组#转变和数组#加入。
var split = samplestr.split('.');
var finalarray = [];
if(split.length > 4) {
finalarray[0] = split.shift();
finalarray[1] = split.shift();
finalarray[2] = split.shift();
finalarray[3] = split.join(".");
}
#2
2
simply replace
简单的替换
finalarray[3] = split[3]+"."split[4];
with
与
finalarray[3] = split.slice(3).join(".");
#1
2
With Array#shift
and Array#join
.
与数组#转变和数组#加入。
var split = samplestr.split('.');
var finalarray = [];
if(split.length > 4) {
finalarray[0] = split.shift();
finalarray[1] = split.shift();
finalarray[2] = split.shift();
finalarray[3] = split.join(".");
}
#2
2
simply replace
简单的替换
finalarray[3] = split[3]+"."split[4];
with
与
finalarray[3] = split.slice(3).join(".");