DEV Community

Deva I
Deva I

Posted on

Objects & Array Scenario Questions

1. User Profile Update

const user = {

name: "Vijay",

age: 25,

email: "ideva4739@gmail.com"

};

▪️Update email and a new property is active = true.

PROGRAM:

EXPLANATION:

🔹In this question first condition update a new email instead of existing email.

🔹If you want update a values give object name and update value in between (.)dot operator.

▪️dot operator is used to            seperate the object name and key name.
Enter fullscreen mode Exit fullscreen mode

🔹So, give user.email= ideva537@gmail.com.

🔹Then add a new element is active= true.

🔹So, create new variable user.isActive= true.

🔹Then print the user.

OUTPUT:

2. Shopping Cart Total

const cart = [

(name: "Shirt", price: 500).

(name: "Shoes", price: 1500).

(name: "Cap", price: 300)

▪️Calculate total price.

PROGRAM:

OUTPUT:

3. Find Specific Object

const users =[
{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:

OUTPUT:

4. Add Item to Array

const products= [
{name: "Shirt", price: 500},
{name: "Shoes", price: 1500}
];
▪️Add a new property to an existing array.

▪️Remove the product name "Shoes" from the array.

▪️From product list return price > 1000 items only.

PROGRAM:

OUTPUT:

5. Count Items in Object

const fruits = {
apple: 2,
banana: 5,
mango: 3
};

▪️Find the total number of fruits.

PROGRAM:

OUTPUT:

6. Convert Array to Object

const users = ["Vijay", "Arun", "Kumar"];

▪️Convert into:

{

Vijay: true,

Arun: true,

Kumar: true
Enter fullscreen mode Exit fullscreen mode

}

PROGRAM:

OUTPUT:

Top comments (0)