-1
var array1 = ["hello", "sam", "how"];

var emptyArray2 = [];

array1.join(" ");

I need to take out first letter and last letter of every element in array1 and push it to emptyArray2. I dont know what to do after converting it to a string. I think i need to use a for loop to go through it , then how would I pull out the first and last letter in every element and push to emptyArray2?

user3525853
  • 25
  • 1
  • 4

4 Answers4

2

Loop through the array and return the string minus the first char and last char. The map function will create a new array, the substring takes all the characters of index 1 to the total length of the string - 1.

var array2 =  ["hello", "sam", "how"].map(function(p){return p.substring(1, p.length - 1)})
bobthedeveloper
  • 3,733
  • 2
  • 15
  • 31
1
var array1 = ["hello", "sam", "how"], result = [];
array1.forEach(function(currentString) {
    result.push(currentString.charAt(0));  // First character
    result.push(currentString.charAt(currentString.length - 1)); //Last character
});
console.log(result);
# [ 'h', 'o', 's', 'm', 'h', 'w' ]
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • You should use `.charAt()` not string indexing. – jfriend00 Apr 16 '14 at 07:49
  • 1
    @jfriend00 Sure, but could you tell me why? – thefourtheye Apr 16 '14 at 07:50
  • @thefourtheye, see [`string.charAt(x) or string[x]?`](http://stackoverflow.com/questions/5943726/string-charatx-or-stringx) Basically, IE7 doesn't support string indexing. – Frédéric Hamidi Apr 16 '14 at 07:52
  • 1
    @jfriend00 If you're thinking about non BMP chars, I think that charAt is as limited as string indexing – Denys Séguret Apr 16 '14 at 07:52
  • Supporting IE7 doesn't seem to me a valid reason to use the verbose `charAt`. You know the other parts of your web app don't support IE7 either so why bother ? – Denys Séguret Apr 16 '14 at 07:54
  • 1
    Mostly it's because `[]` for strings was not part of the early standards and thus not universally supported. But, also because accessing via `var chr = str[x]` implies you can assign via `str[x] = "f"` which you cannot (just fails silently) so I think it makes people more likely to make mistakes thinking something works like an array when it doesn't. I guess I'll say it's more a style choice now unless you want to support IE7 in which case you have to use `.charAt()`. – jfriend00 Apr 16 '14 at 07:55
1
var array1 = ["hello", "sam", "how"];

var emptyArray2 = [];

array1.forEach(function(item){
 //item.charAt(0) will give character at start of string
 //item.charAt(item.length-1) will give character at end of string
 emptyArray2.push(item.charAt(0),item.charAt(item.length-1));
 //Array.push(..) is to append elements into the array
});
console.log(emptyArray2); // will print ["h","o","s","m","h","w"] in console
vivek_nk
  • 1,590
  • 16
  • 27
0

More ways to do this:

// Original array
let array = [ "hello", "sam", "how" ];


// Finding the first letters from the array items.
let firstLetters1 = array.map(eachItemInArray => {
    return eachItemInArray[0];
})

// DEBUG: Display the result
console.log("First letters of items in the array are: ");
console.log(firstLetters1);

// Another short cut way of writing the above code in a single line.
let firstLetters2 = array.map(eachItemInArray => eachItemInArray[0]);

// DEBUG: Display the result
console.log("First letters of items in the array are (short cut method): ");
console.log(firstLetters2);


// Finding the last letters from the array items.
let lastLetters1 = array.map(eachItemInArray => {
    let index = eachItemInArray.length - 1;
    return eachItemInArray[index];
})

// DEBUG: Display the result
console.log("Last letters of items in the array are: ");
console.log(lastLetters1);

// Another short cut way of writing the above code in a single line
let lastLetters2 = array.map(eachItemInArray => eachItemInArray[eachItemInArray.length - 1]);

// DEBUG: Display the result
console.log("Last letters of items in the array are (short cut method): ");
console.log(lastLetters2);


// Create a new Array for first & last characters
let newArray = [];

for (let i = 0; i < firstLetters1.length; i++) {
    newArray.push(firstLetters1[i]);
    newArray.push(lastLetters1[i]);
}

// DEBUG: Display the original array & the new array of characters
console.log("\nOriginal array: ");
console.log(array);
console.log("Array of first and last characters: ");
console.log(newArray);

I have shown the long way of using the map and a one-line way of using the map in JavaScript.

myverdict
  • 622
  • 8
  • 24