0

I want split array value .

for example

gUser[1] = CAR.BENZ.CCLASS.1962

gUser[2] = CAR.PORSCHE.911.2001

I want get string only BENZ.CLASS.1962 and PORSCHE.911.2001

How to split array value on java script?

@update.

not always CAR string. so, not use substring.

7
  • you want to get a substring of the elements. Commented Jul 5, 2017 at 5:45
  • @davidxxx not use substring, because not always CAR string Commented Jul 5, 2017 at 5:51
  • so you need to split it from the first . (dot)? @hyunwooks Commented Jul 5, 2017 at 5:51
  • @john is possible use split on array? Commented Jul 5, 2017 at 5:52
  • 1
    You can use it on a string. (w3schools.com/jsref/jsref_split.asp) Commented Jul 5, 2017 at 5:56

8 Answers 8

2

You can use map to access each string in array then use replace. Use a regex to match string before '.' and replace only the first match, like this:

  var gUser = ['CAR.BENZ.CCLASS.1962', 'CAR.PORSCHE.911.2001'];

  var gUserModified = gUser.map(function(v){
        return v.replace(/[^\.]+\./, '');
  });
  console.log(gUserModified);

Sign up to request clarification or add additional context in comments.

Comments

1

Split it with dot then slice it and join it with dot

var gUser =[];
gUser[1] = "CAR.BENZ.CCLASS.1962";

gUser[2] = "CAR.PORSCHE.911.2001";

console.log(gUser[1].split('.').slice(1).join('.'));
console.log(gUser[2].split('.').slice(1).join('.'));

Comments

0

From your question, it's not clear if the array is a string array

If it is a string array you can do:

ES6+: gUser.map((user)=>{return user.split("CAR.")[1]})

ES5: gUser.map(function(user){return user.split("CAR.")[1]});

Comments

0

The below code is not tested but should probably work, with maybe minor tweaks

var splitStr = ''
var correctCarArr = []
for(let i = 0; i < gUser.length; i++){
  splitStr = gUser[i].split('.')
  let temp = ''
  for(let j = 1; j < splitStr.length; j++){
    temp += splitStr[j]
  }
  correctCarArr.push(temp)
}

Comments

0

var gUser = [];
gUser[1] = "CAR.BENZ.CCLASS.1962";
var gu1 = gUser[1].split(".");
gu1.shift();
console.log(gu1.join("."));

Comments

0

So here is the way without using any regex by only using string and array methods.

const gUser = ['CAR.BENZ.CCLASS.1962', 'CAR.PORSCHE.911.2001', 'XYZAB.PORSCHE.YSA.2021']

for (let i = 0; i < gUser.length; i++) {
  console.log('Required String: ', gUser[i].split('.').slice(1).join('.'));
}

What we do is, we split the string into parts where . is encountered.

gUser[0].split('.') returns ['CAR', 'BENZ', 'CCLASS', '1962']

Later when slice(1) is called, the zeroth element of array is chopped off and will return ['BENZ', 'CCLASS', '1962']

And finally using join('.'), we merge the array elements to a single string with a . between each element, which returns BENZ.CCLASS.1962


Hope this helps! :)

Comments

0

Its easier split then shift the array to remove the first item like this:

gUser = ["CAR.BENZ.CCLASS.1962"];
var benz = gUser[0].split(".");
benz.shift();
alert(benz.join('.'));

There are other options from shift like slice(1) but In terms of performance, shift is apparently faster https://jsperf.com/shift-pop-vs-slice/4

Comments

0

Something Like This

`for(let index = 0; index < gUser.length; index++) {
    console.log(gUser[index].split('.').splice(0, 1).join('.'));
}`

I haven't tested it. Please check and let me know

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.