Skip to content
Laviyx Learn

Try

JavaScript Arrays and Objects Practice

Practise transforming data with map, filter, reduce and sort, working with objects, destructuring, Map, Set and JSON. Every solution has been run with Node.js and shows its real output.

Intermediate7 problemsUpdated

Review first: Arrays, Array Methods: map, filter, reduce, Objects, Destructuring and Spread, Map and Set, JSON

Several problems use this list of orders:

JavaScript
const orders = [
  { id: 1, customer: "Asha", amount: 499, status: "paid" },
  { id: 2, customer: "Ravi", amount: 1200, status: "pending" },
  { id: 3, customer: "Asha", amount: 250, status: "paid" },
  { id: 4, customer: "Meera", amount: 80, status: "cancelled" },
];
  1. 2.Sorted summary strings

    Beginner

    Print an array of strings like "#2 Ravi ₹1200" for every order that is not cancelled, sorted from the largest amount to the smallest.

    Show hint

    filter, then toSorted((a, b) => b.amount - a.amount), then map to strings.

    Show solution
    JavaScript
    const orders = [
      { id: 1, customer: "Asha", amount: 499, status: "paid" },
      { id: 2, customer: "Ravi", amount: 1200, status: "pending" },
      { id: 3, customer: "Asha", amount: 250, status: "paid" },
      { id: 4, customer: "Meera", amount: 80, status: "cancelled" },
    ];
    const summary = orders
      .filter((o) => o.status !== "cancelled")
      .toSorted((a, b) => b.amount - a.amount)
      .map((o) => `#${o.id} ${o.customer} ₹${o.amount}`);
    console.log(summary);
    Output
    [ '#2 Ravi ₹1200', '#1 Asha ₹499', '#3 Asha ₹250' ]
  2. 3.Totals per customer

    Intermediate

    Build an object that maps each customer's name to the total of all their orders (any status), and print it.

    Show hint

    reduce into an object: totals[name] = (totals[name] ?? 0) + amount.

    Show solution
    JavaScript
    const orders = [
      { id: 1, customer: "Asha", amount: 499, status: "paid" },
      { id: 2, customer: "Ravi", amount: 1200, status: "pending" },
      { id: 3, customer: "Asha", amount: 250, status: "paid" },
      { id: 4, customer: "Meera", amount: 80, status: "cancelled" },
    ];
    const totals = orders.reduce((acc, { customer, amount }) => {
      acc[customer] = (acc[customer] ?? 0) + amount;
      return acc;
    }, {});
    console.log(totals);
    Output
    { Asha: 749, Ravi: 1200, Meera: 80 }
  3. 4.Unique customers

    Beginner

    Print the unique customer names in the order they first appear, and how many there are.

    Show hint

    new Set(...) removes duplicates; spread it back into an array.

    Show solution
    JavaScript
    const orders = [
      { id: 1, customer: "Asha", amount: 499, status: "paid" },
      { id: 2, customer: "Ravi", amount: 1200, status: "pending" },
      { id: 3, customer: "Asha", amount: 250, status: "paid" },
      { id: 4, customer: "Meera", amount: 80, status: "cancelled" },
    ];
    const customers = [...new Set(orders.map((o) => o.customer))];
    console.log(customers, customers.length);
    Output
    [ 'Asha', 'Ravi', 'Meera' ] 3
  4. 5.Update an object without changing it

    Intermediate

    Given const user = { name: "Asha", address: { city: "Pune", pin: "411001" } }, create moved, a copy where the city is "Delhi", without changing user. Print both cities.

    Show hint

    Spread the object and the nested object: { ...user, address: { ...user.address, city: "Delhi" } }.

    Show solution
    JavaScript
    const user = { name: "Asha", address: { city: "Pune", pin: "411001" } };
    const moved = { ...user, address: { ...user.address, city: "Delhi" } };
    console.log(user.address.city, moved.address.city);
    Output
    Pune Delhi
  5. 6.Most common words

    Intermediate

    Count how often each word appears in "the cat and the hat and the bat" using a Map, then print the two most common words with their counts.

    Show hint

    Count with map.set(word, (map.get(word) ?? 0) + 1), then sort [...map] by count.

    Show solution
    JavaScript
    const text = "the cat and the hat and the bat";
    const counts = new Map();
    for (const word of text.split(" ")) {
      counts.set(word, (counts.get(word) ?? 0) + 1);
    }
    const top = [...counts].sort((a, b) => b[1] - a[1]).slice(0, 2);
    console.log(top);
    Output
    [ [ 'the', 3 ], [ 'and', 2 ] ]
  6. 7.JSON round trip

    Beginner

    Convert { course: "JavaScript", lessons: 56, tags: ["web", "frontend"] } to a JSON string and print it, then parse it back and print the second tag.

    Show hint

    JSON.stringify and JSON.parse.

    Show solution
    JavaScript
    const data = { course: "JavaScript", lessons: 56, tags: ["web", "frontend"] };
    const json = JSON.stringify(data);
    console.log(json);
    console.log(JSON.parse(json).tags[1]);
    Output
    {"course":"JavaScript","lessons":56,"tags":["web","frontend"]}
    frontend