0

I am using nodejs and I get an array using walksync i.e.

var paths = ['Essential Classes.jade' , 'introduction.jade']

These are filenames inside a folder. Now i want only filename but not extension, So I have to split every string in array. But don't know how.I want the result like below

['Essential Classes' , 'introduction']

3 Answers 3

4

You can do

var paths = ['Essential Classes.jade' , 'introduction.jade'];

paths = paths.map(e => e.replace(/\.\w+$/, ''));

console.log(paths);

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

1 Comment

Thanks guys. You are awsome
2

You can use map to split each string in the array and get the first string, which is the file name:

1- Using an arrow function:

var paths = ['Essential Classes.jade' , 'introduction.jade'],

paths = paths.map((str) => str.split('.')[0]);
console.log(paths);

2- Using a normal function:

var paths = ['Essential Classes.jade' , 'introduction.jade'],

paths = paths.map(function(str) { return str.split('.')[0] });
console.log(paths);

8 Comments

Wow, awesome. Thanks
Glad to help :).
can you tell me about =>
@SyedMuhammadAsad (arguments) => expression is an arrow function, which is similar to function(arguments) { expression }.
Wow. Now its short. Thanks
|
0
  1. use . each to iterate the array
  2. use split() to split the array value then get first index

var paths = ['Essential Classes.jade', 'introduction.jade'];

var arr = [];


$.each(paths, function(i,v) {

  arr.push(v.split(".")[0])

})

console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.