-3

How can I check if one array contains arr in JavaScript? Usually, I would expect a subString method, but there doesn't seem to be one. What is a reasonable way to check for this?

arr=["aaaaaa-2","bbbbbbbb-2","zzzz-3","ffddssaa-1","sssssss-3","areyo-1"]

i trying to remove last two characters from list, i tried with below code but no use of these, can any one suggest me

arr.substring(0, -2)

I need array like

["aaaaaa","bbbbbbbb","zzzz","ffddssaa","sssssss","areyo"]
Pranav C Balan
  • 110,383
  • 23
  • 155
  • 178
  • arr.substring(0, -2) this will work, just modify arr[index].substring(0, -2) – Sinto May 12 '16 at 10:51
  • Possible duplicate of [Javascript chop/slice/trim off last character in string](http://stackoverflow.com/questions/952924/javascript-chop-slice-trim-off-last-character-in-string) – Ahmad Sharif May 12 '16 at 10:52

7 Answers7

3

Just try with:

arr.map(function(v){
  return v.substr(0, v.length - 2);
});
hsz
  • 143,040
  • 58
  • 252
  • 308
1

Iterate over the elements and apply the substring() method, for that use map()

var arr = ["aaaaaa-2", "bbbbbbbb-2",, "zzzz-3", "ffddssaa-1", "sssssss-3", "areyo-1"];

var res = arr.map(function(v) {
  return v.substring(0, v.length-2);
});

console.log(res);
Pranav C Balan
  • 110,383
  • 23
  • 155
  • 178
0
var arr=["aaaaaa-2","bbbbbbbb-2","zzzz-3","ffddssaa-1","sssssss-3","areyo-1"];

for(var i in arr){
  consile.log(arr[i].substr(0,arr[i].lastIndexOf("-")); //here is your value
}
Anupam Singh
  • 1,148
  • 13
  • 25
0

use map method of array.

arr = arr.map(function(item){return item.slice(0, item.length-2)});
CrazyMax
  • 131
  • 1
  • 5
0
for(var i=0;i<arr.length;i++) {
    arr[i] = arr[i].substring(0, arr[i].length-2);
}
take
  • 2,174
  • 2
  • 18
  • 36
0

Easiest thing would be to use map function

var arr=["aaaaaa-2","bbbbbbbb-2","zzzz-3","ffddssaa-1","sssssss-3","areyo-1"];
console.log(arr.map(function (e){
    return e.slice(0, -2);
}));
Wild Widow
  • 2,159
  • 3
  • 19
  • 33
0

first of all for your information substring method work on string not on array and here arr is an array the code should be like:-

for(var i = 0;i<arr.length;i++)
   {
     arr[i].substring(0, arr[i].length-2);
   }
Rahul
  • 229
  • 2
  • 6