0

I am trying to get text from an array except year,month,date using javascript.I do not know how do it.

var arr = [
  "power-still-rate-19.08.22", 
  "main-still-rate-19.08.22", 
  "oil-power-rate-19.08.22", 
  "oil-mill-rate-19.7.2"
];
var result;

for (var i = 0; i < arr.length; i++) {
  result = arr[i].remove('?????????');
}
console.log(result);

//result should be like = power-still-rate,main-still-rate,oil-power-rate ;

1
  • Sometime date will be like 19-8-2.So How we can do in this condition? Commented Aug 22, 2019 at 7:36

5 Answers 5

3

Split, slice and join

var arr = ["power-still-rate-19.08.22", "main-still-rate-19.08.22", "oil-power-rate-19.08.22","oil-mill-rate-19.7.2"];
var result = arr.map(item => item.split("-").slice(0,-1).join("-"))
console.log(result);

Split, pop and join

var arr = ["power-still-rate-19.08.22", "main-still-rate-19.08.22", "oil-power-rate-19.08.22","oil-mill-rate-19.7.2"];
var result = arr.map(item => { let res = item.split("-"); res.pop(); return res.join("-") })
console.log(result);

No map:

var arr = ["power-still-rate-19.08.22", "main-still-rate-19.08.22", "oil-power-rate-19.08.22","oil-mill-rate-19.7.2"];
var result = arr.join("").split(/-\d{1,}\.\d{1,}\.\d{1,}/);
result.pop(); // last empty item, not needed if you do not want an array just join with comma
console.log(result);

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

2 Comments

Can we do without map?
I have updated my question..Sometime date will be like 19-8-2. month and date will be single digit so how we can do on that condition?
1

Use a regular expression to match non-digit characters from the start of the string, followed by - and a digit:

const input = ["power-still-rate-19.08.22", "main-still-rate-19.08.22", "oil-power-rate-19.08.22","oil-mill-rate-19.7.2"];
const output = input.map(str => str.match(/\D+(?=-\d)/)[0]);
console.log(output);

Comments

0

Using split on - ,splicing the last element which is the date and joining on -

var arr=["power-still-rate-19.08.22","main-still-rate-19.08.22","oil-power-rate-19.08.22"];
    arr.forEach(function(e,i){
    arr[i]=e.split('-').splice(0,3).join('-')
    })
    console.log(arr)

Comments

0

You can remove back string by using slice function and join them with join function.

var arr = ["power-still-rate-19.08.22", "main-still-rate-19.08.22", "oil-power-rate-19.08.22","oil-mill-rate-19.7.2"];
var result = arr.map(str => str.slice(0, str.lastIndexOf('-'))).join(',');
console.log(result);

Comments

0

You can use String#replace method to remove certain pattern from string using RegExp.

const input = ["power-still-rate-19.08.22", "main-still-rate-19.08.22", "oil-power-rate-19.08.22","oil-mill-rate-19.7.2"];
const res = input.map(str => str.replace(/-\d{1,2}\.\d{1,2}\.\d{1,2}$/, ''));
console.log(res);

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.