Medium

Iterating Over Object Properties in JavaScript

Description

Objects in JavaScript store data as key-value pairs. Sometimes, you need to iterate over these properties to perform operations like filtering, transforming, or summarizing the data.

In this challenge, you will create a function that takes an object and returns an array of strings, where each string represents a key-value pair in the format "<key>: <value>".

Example Object Structure:

const person = {
name: "Alice",
age: 25,
occupation: "Software Engineer"
};

Your task is to complete the function formatObjectProperties, which should take an object as a parameter and return an array of formatted strings.

Example Usage:

const person = {
name: "John",
age: 30,
occupation: "Designer"
};
console.log(formatObjectProperties(person)); // Expected output: ["name: John", "age: 30", "occupation: Designer"]

Constraints:

  • The input will always be a valid object with at least one key-value pair.
  • The function should return an array of strings, where each string is in the format "<key>: <value>".
  • The order of the strings in the array should match the order of the properties in the object.

window code 2Test Cases

Input:

{
  "city": "New York",
  "population": 8419000
}

Expected Output:

[
  "city: New York",
  "population: 8419000"
]