Reputation: 521
I came across this piece of code in Underscore.js, I want to convert this to vanilla javascript. Any idea on how to do that ?
var makeLetters = function(word) {
return _.map(word.split(''), function(character) {
return { name: character, chosen: false };
});
}
This is the current output from the above function, I want to retain the same structure, except I want to accomplish this in regular javascript.
0: {name: "s", chosen: false, $$hashKey: "003"}
1: {name: "c", chosen: false, $$hashKey: "004"}
2: {name: "o", chosen: false, $$hashKey: "005"}
3: {name: "p", chosen: false, $$hashKey: "006"}
4: {name: "e", chosen: false, $$hashKey: "007"}
Upvotes: 0
Views: 1045
Reputation: 455
var makeLetterWord="scope";
var makeLettersSplit = makeLetterWord.split('').map(function(character) {
return { name: character, chosen: false };
});
console.log(makeLettersSplit );
Upvotes: 0
Reputation: 3737
// Your current Underscore code:
/*
var makeLetters = function(word) {
return _.map(word.split(''), function(character) {
return { name: character, chosen: false };
});
*/
// New ES6+ code:
const makeLetters = (word) => word.split('').map(character => ({ name: character, chosen: false }));
// If you aren't using ES6+:
var makeLetters = function(word) {
return word.split('')
.map(function(character) {
return { name: character, chosen: false };
});
};
Upvotes: 5
Reputation: 1261
In ES 2015+
function makeLetters(word) {
return word.split('').map(v => { name: v, chosen: false });
}
In pre ES 5
function makeLetters(word) {
return word.split('').map(function(v) {
return { name: v, chosen: false };
});
}
Upvotes: -1