Skip to content

Instantly share code, notes, and snippets.

@n0m4dz
Last active November 9, 2016 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n0m4dz/c9bef4b0a9d5a55bf0a818d2f6bcd0d5 to your computer and use it in GitHub Desktop.
Save n0m4dz/c9bef4b0a9d5a55bf0a818d2f6bcd0d5 to your computer and use it in GitHub Desktop.
Map and Filter in ES6
var numbers = [1, 2, 3, -1, -2, -3];
var newMapNumbers = numbers.map((number) => {
if(number > 0){
number = number * 10
}
return number
})
var newFilterNumbers = numbers.filter((number) => {
if(number > 0){
return number;
}
})
console.log('Map values', newMapNumbers)
console.log('Filter values', newFilterNumbers)
function map(objList){
var newArr = [];
objList.foreach(function(obj){
if(obj.id == 1){
obj.job = 'New job';
}
newArr.push(obj);
})
return newArr;
}
function filter(objList){
var newArr = [];
objList.foreach(function(obj){
if(obj.id == 1){
obj.job = 'New job';
newArr.push(obj);
}
})
return newArr;
}
var newValues = map(todos);
var todos = [
{
id: 1,
job: 'Get up',
completed: true
},
{
id: 2,
job: 'Go to work',
completed: true
},
{
id: 3,
job: 'Have lunch',
completed: false
},
{
id: 4,
job: 'Go back to home',
completed: false
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment