Write a function to flatten objects

Using ES6, write a function ‘flattenObjectArray’ that takes an array of objects and returns a single array consisting of the object keys and values.

var data = [{ name: ‘Ben’, age: 30 }, { name: ‘Jack’, age: 40 }];

flattenObjectArray(data);

Should return

[“name”, “Ben”, “age”, 30, “name”, “Jack”, “age”, 40]

What should be the right approach to achive this ? many thanks

Well, what is the logical pattern here? You have an array of objects, and you need to return an array of the format [key,value,key,value]. Where to begin?

  • How can you iterate over each object in the array?
  • How can you iterate over the keys (property names) for each object?
  • How can you place the key on an array?
  • How about the value for that key?

Those are the answers you seek, young padawan. Well, those are the questions. Seek the answers. :wink: