1. User Profile Update
You have:
const user = {
name:"vijay",
age:25,
email:"vijay@gmail.com"};
Update email and a new property isActive = true.
PROGRAM:
const user = {name:"vijay",
age: 25,
email: "vijay@gmail.com"
}
user.email = "mohan2004@gmail.com"
user.isActive = true
console.log(user)
OUTPUT:
{
name: 'vijay',
age: 25,
email: 'mohan2004@gmail.com',
isActive: true
}
2. Shopping Cart Total
You have:
const cart = [
{name: "shirt",price : 500},
{name: "shoes",price : 1500},
{name :"cap", price : 300}
];
Calculate total price.
PROGRAM:
const cart = [
{name: "shirt",price : 500},
{name: "shoes",price : 1500},
{name :"cap", price : 300}
];
const sum = cart.map(val=> val.price).reduce((total,val)=> total+val)
console.log("total :", sum );
OUTPUT:
total : 2300
3. Find Specific Object
You have:
const user = [
{name: "vijay", age: 25},
{name: "Arun", age: 30},
{name: "Kumar", age: 28}
];
From an array of users,find the user whose name is "Arun".
PROGRAM:
const user = [
{name: "vijay", age: 25},
{name: "Arun", age: 30},
{name: "Kumar", age: 28}
];
function nam(user){
return user.name == "Arun"
}
const result = user.filter(nam);
console.log(result);
OUTPUT:
[ { name: 'Arun', age: 30 } ]
4. Add item to array
You have:
const products = [{name: "shirt", price:1500},
{name: "shoes", price:1500}];
Add a new product dynamically to an existing product array remove a product with the name "shoes" from the array.From a product list,return only items with price > 1000.
PROGRAM:
const products = [{name: "shirt", price:1500},{name: "shoes", price:1500}];
products.push({name:"watch", price:1700})
const sum = products.filter(val=>val.name!="shoes"&&val.price>1000)
console.log(sum);
OUTPUT:
[ { name: 'shirt', price: 1500 }, { name: 'watch', price: 1700 } ]
5. Count items in object
You have:
const fruits = {
apple:2,
banana:5,
mango:3
};
Find the total number of fruits.
PROGRAM:
const fruits = {
apple:2,
banana:5,
mango:3
};
const newarr = Object.values(fruits)
function arr1(total,value){
return total + value
}
const arr = newarr.reduce(arr1)
console.log("fruits total:",arr)
OUTPUT:
fruits total: 10
6. Convert array to object
You have:
const users = ["vijay","arun","kumar"];
Convert into:
{ vijay:true,
Arun:true,
Kumar:true }
PROGRAM:
const users = ["vijay","arun","kumar"];
function arr(value){
return [value,"true"]
}
const result = users.map(arr)
const obj = Object.fromEntries(result)
console.log(obj);
OUTPUT:
{ vijay: 'true', arun: 'true', kumar: 'true' }
Top comments (0)