0

I have below array of objects:

var all = [
{f1: "v1", f2: "v2"},
{f1: "v1", f2: "v2", f3: "v3"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4", fn: "vn"}
];

Desired Output:

[f1, f2, f3, f4, f5, ...., fn];

Currently Using:

all.reduce((sum, item) => ([...new Set([...sum, ...Object.keys(item)])]), []);

Working Example: codepen

Any suggestions using es6 or any new js feature, for better performance.

5
  • It's not 100% clear what you're trying to achieve your example has 5 results from an array of 4 objects. Are you trying to get the final key of the object. Is order guaranteed? Commented Oct 13, 2021 at 20:32
  • Yet another option: new Set(all.map(o => Object.keys(o)).flat()) Commented Oct 14, 2021 at 8:07
  • @DamianGreen, desired output is "array of keys of heighest key:value pair object from the given array". Commented Oct 15, 2021 at 7:43
  • @FelixKling will it be havening better performance, as again we are having 4 operations. Commented Oct 15, 2021 at 7:45
  • "desired output is "array of keys of heighest key:value pair object from the given array" That's not the same as getting the union of all keys. What is it now? "will it be havening better performance, as again we are having 4 operations" It doesn't create intermediate objects like your solution. But in the end you'll have to actually measure the performance of different solutions. Commented Oct 15, 2021 at 7:59

3 Answers 3

6

You can flatten them into a single object, then take the keys.

var all = [
{f1: "v1", f2: "v2"},
{f1: "v1", f2: "v2", f3: "v3"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4", fn: "vn"}
];

const output = Object.keys(Object.assign({}, ...all));
console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

0
var all = [
{f1: "v1", f2: "v2"},
{f1: "v1", f2: "v2", f3: "v3"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4", fn: "vn"}
];

console.log(Object.keys(all[all.length-1]))

3 Comments

This makes an assumption that the last element in the array contains all the keys and all the other elements in the array can be ignored. It's not clear that's a safe assumption.
No, any object of the array can have heighest number of key:value pairs, not the last one always.
I know, but for the given input it shows the right results, why care about conercases if not asked?
0

Using Array.flatMap and a Set object to get a Set of unique keys and then use the spread operator to convert the Set back into an array.

var all = [
{f1: "v1", f2: "v2"},
{f1: "v1", f2: "v2", f3: "v3"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4"},
{f1: "v1", f2: "v2", f3: "v3", f4: "v4", fn: "vn"}
];

const res = [...new Set(all.flatMap(Object.keys))];
console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.