Back to Exercises
Objects & JSON
20 Exercises Available
Object access, defaults, deep-ish patterns, JSON safe.
1.Easy
Safe Nested Read (optional chaining)
Function `getCity(user)` banao jo `user.address.city` return kare. Missing ho to "Unknown".
Sample Output:
getCity({address: {city: "Mumbai"}}) => "Mumbai"
getCity({address: {}}) => "Unknown"
getCity(null) => "Unknown"View Solution
2.Medium
Pick Keys
Create a function `pick(obj, keys)` that keeps only given keys in a new object.
Sample Output:
pick({a: 1, b: 2, c: 3}, ["a", "c"]) => {a: 1, c: 3}
pick({x: 10, y: 20}, ["z"]) => {}View Solution
3.Easy
Merge Defaults (without mutation)
Function `withDefaults(config, defaults)` banao. Config overrides defaults. New object return karo.
Sample Output:
withDefaults({b: 20}, {a: 1, b: 2}) => {a: 1, b: 20}
withDefaults({}, {x: 10}) => {x: 10}View Solution
4.Easy
Safe JSON Parse
Create a function `safeJsonParse(str)`. Return null for invalid input.
Sample Output:
safeJsonParse('{"a":1}') => {a: 1}
safeJsonParse('invalid') => null
safeJsonParse('[1,2,3]') => [1, 2, 3]View Solution
5.Medium
Count by Key
Given array of users `{role}`, function `countByRole(users)` banao.
Output: `{ admin: 2, user: 5 }`
Sample Output:
countByRole([{role:"admin"},{role:"user"},{role:"admin"}]) => {admin: 2, user: 1}
countByRole([{role:"user"}]) => {user: 1}View Solution
6.Easy
Omit Keys from Object
Function `omit(obj, keys)` banao jo object se specified keys remove kare.
Example: omit({a: 1, b: 2, c: 3}, ["a", "c"]) => {b: 2}
Sample Output:
omit({a: 1, b: 2, c: 3}, ["a", "c"]) => {b: 2}
omit({x: 1, y: 2}, ["x"]) => {y: 2}View Solution
7.Medium
Flatten Object (one level)
Function `flattenObject(obj)` banao jo nested object ko one level flatten kare.
Example: flattenObject({a: 1, b: {c: 2}}) => {a: 1, "b.c": 2}
Sample Output:
flattenObject({a: 1, b: {c: 2}}) => {a: 1, "b.c": 2}
flattenObject({x: {y: {z: 1}}}) => {"x.y.z": 1}View Solution
8.Hard
Deep Merge Objects
Function `deepMerge(target, source)` banao jo dono objects ko deeply merge kare.
Example: deepMerge({a: 1, b: {c: 2}}, {b: {d: 3}}) => {a: 1, b: {c: 2, d: 3}}
Sample Output:
deepMerge({a: 1, b: {c: 2}}, {b: {d: 3}}) => {a: 1, b: {c: 2, d: 3}}
deepMerge({x: 1}, {x: 2}) => {x: 2}View Solution
9.Medium
Get Nested Property Value
Function `get(obj, path, defaultValue)` banao jo nested object se property value get kare using path string.
Example: get({a: {b: {c: 1}}}, "a.b.c") => 1
Sample Output:
get({a: {b: {c: 1}}}, "a.b.c") => 1
get({a: {b: 1}}, "a.b.c", "default") => "default"View Solution
10.Hard
Set Nested Property Value
Function `set(obj, path, value)` banao jo nested object me path string se value set kare.
Example: set({}, "a.b.c", 1) => {a: {b: {c: 1}}}
Sample Output:
set({}, "a.b.c", 1) => {a: {b: {c: 1}}}
set({x: 1}, "y.z", 2) => {x: 1, y: {z: 2}}View Solution
11.Medium
Transform Object Keys
Function `transformKeys(obj, fn)` banao jo object ke sabhi keys ko transform kare.
Example: transformKeys({name: "John", age: 30}, k => k.toUpperCase()) => {NAME: "John", AGE: 30}
Sample Output:
transformKeys({name: "John", age: 30}, k => k.toUpperCase()) => {NAME: "John", AGE: 30}
transformKeys({a: 1}, k => `prefix_${k}`) => {prefix_a: 1}View Solution
12.Easy
Invert Object (key-value swap)
Function `invert(obj)` banao jo object ke keys aur values ko swap kare.
Example: invert({a: 1, b: 2}) => {1: "a", 2: "b"}
Sample Output:
invert({a: 1, b: 2}) => {1: "a", 2: "b"}
invert({x: "hello"}) => {hello: "x"}View Solution
13.Easy
Check if Object is Empty
Function `isEmpty(obj)` banao jo check kare ki object empty hai ya nahi.
Example: isEmpty({}) => true, isEmpty({a: 1}) => false
Sample Output:
isEmpty({}) => true
isEmpty({a: 1}) => false
isEmpty(null) => trueView Solution
14.Easy
Map Object Values
Function `mapValues(obj, fn)` banao jo object ke sabhi values ko transform kare.
Example: mapValues({a: 1, b: 2}, x => x * 2) => {a: 2, b: 4}
Sample Output:
mapValues({a: 1, b: 2}, x => x * 2) => {a: 2, b: 4}
mapValues({x: "hello"}, s => s.toUpperCase()) => {x: "HELLO"}View Solution
15.Easy
Filter Object by Condition
Function `filterObject(obj, fn)` banao jo object ke entries ko condition ke basis par filter kare.
Example: filterObject({a: 1, b: 2, c: 3}, (v, k) => v > 1) => {b: 2, c: 3}
Sample Output:
filterObject({a: 1, b: 2, c: 3}, (v, k) => v > 1) => {b: 2, c: 3}
filterObject({x: "a", y: "b"}, (v, k) => k === "x") => {x: "a"}View Solution
16.Easy
Get Object Size (key count)
Function `objectSize(obj)` banao jo object me keys ki count return kare.
Example: objectSize({a: 1, b: 2, c: 3}) => 3
Sample Output:
objectSize({a: 1, b: 2, c: 3}) => 3
objectSize({}) => 0View Solution
17.Easy
Find Key by Value
Function `findKey(obj, value)` banao jo object me value ke corresponding key return kare.
Example: findKey({a: 1, b: 2, c: 1}, 1) => "a" (first match)
Sample Output:
findKey({a: 1, b: 2, c: 1}, 1) => "a"
findKey({x: "hello"}, "hello") => "x"View Solution
18.Medium
Sort Object by Values
Function `sortByValues(obj)` banao jo object ke entries ko values ke basis par sort kare.
Example: sortByValues({a: 3, b: 1, c: 2}) => {b: 1, c: 2, a: 3}
Sample Output:
sortByValues({a: 3, b: 1, c: 2}) => {b: 1, c: 2, a: 3}
sortByValues({x: 10, y: 5}) => {y: 5, x: 10}View Solution
19.Easy
Compare Two Objects (shallow)
Function `shallowEqual(obj1, obj2)` banao jo dono objects ko shallow compare kare.
Example: shallowEqual({a: 1, b: 2}, {a: 1, b: 2}) => true
Sample Output:
shallowEqual({a: 1, b: 2}, {a: 1, b: 2}) => true
shallowEqual({a: 1}, {a: 2}) => falseView Solution
20.Easy
Create Object from Array of Pairs
Function `fromPairs(pairs)` banao jo array of [key, value] pairs se object create kare.
Example: fromPairs([["a", 1], ["b", 2]]) => {a: 1, b: 2}
Sample Output:
fromPairs([["a", 1], ["b", 2]]) => {a: 1, b: 2}
fromPairs([["x", "hello"]]) => {x: "hello"}View Solution