Compare Two javascript object and array and console respective values.can anyone help me please
const Icons = { mail: 'fa fa-envelope', phone: 'fa fa-phone', user: 'fa fa-user', cart: 'fa fa-cart' }; const icon = [ 'mail', 'phone', 'user', 'cart' ]; icon.map(function(v, i) { console.log(i, Icons.v); });
expected output:
0,fa fa-envelope 1,fa fa-phone 2,fa fa-user 3,fa fa-cart
getting undefined can anyone please tell me whats wrong
You are getting undefined
as Icons
doesn't have a property v
.
Since v
is a variable use Bracket notation . ie Icons[v]
const Icons = { mail: 'fa fa-envelope', phone: 'fa fa-phone', user: 'fa fa-user', cart: 'fa fa-cart' }; const icon = [ 'mail', 'phone', 'user', 'cart' ]; icon.map(function(v, i) { console.log(i, Icons[v]); });
const Icons = {
mail:'fa fa-envelope',
phone:'fa fa-phone',
user:'fa fa-user',
cart:'fa fa-cart'
};
const icon = [
'mail',
'phone',
'user',
'cart'
];
icon.map(function(v,i){
console.log(i,Icons[v]); //try this
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.