Skip to content

Instantly share code, notes, and snippets.

@hotrungnhan
Last active December 23, 2021 11:25
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 hotrungnhan/2390fc5fc406239eccdb25f40b5ca576 to your computer and use it in GitHub Desktop.
Save hotrungnhan/2390fc5fc406239eccdb25f40b5ca576 to your computer and use it in GitHub Desktop.
Map key,value from old object to new key,value object and log it into console.
//example
// obj {
// a:0,
// b:1
// }
// output {
// a1:1,
// a2:2
// }
console.log(JSON.stringify(Object.fromEntries(Object.entries("your object with key value").map(([key,value])=>{
return [key+1,value+1]
})),null,2))
// just code itself
Object.fromEntries(Object.entries("your object with key value").map(([key,value])=>{
return [key+1,value+1]
})),
// function
// calback take 2 parameter and should return an array with length =2 .
function mapObject(obj,calback){
Object.fromEntries(Object.entries(obj,map(([key,value])=>{
return callback(key,value)
})))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment