1

I am trying to get an array of (distinct) keys from an array of objects.

arr =[
   {id: 1, desc: "", name: "", objectives: Array(3), …},
   {id: 2, desc: "", name: "", objectives: Array(3), …},
   {id: 3, desc: "", name: "", objectives: Array(3), …},
   {id: 4, desc: "", name: "", objectives: Array(3), …},
]

Desired output is [id, desc, name, objectives]. I have tried:

Object.keys(arr)[0]
// output [{id: 1, desc: "", name: "", objectives: Array(3), …}]

arr.flatMap(Object.key)
// output ["id", "desc", "name", "objectives", "id", "desc", "name", "objectives", "id", "desc", "name", "objectives", "id", "desc", "name", "objectives"]

I'm sorry if I missed the answer to this when scouring SO. Any help would be really appreciated.

1
  • The keys of just the first object (as per the title) would just be const keys = Object.keys(arr[0]);. Was that what you're asking? Commented Oct 10, 2019 at 0:21

3 Answers 3

2

You can use a Set to weed out the duplicates:

const arr = [
   {id: 1, desc: "", name: "", objectives: []},
   {id: 2, desc: "", name: "", objectives: []},
   {id: 3, desc: "", name: "", objectives: []},
   {id: 4, desc: "", name: "", objectives: []},
];

const keys = [...new Set(arr.flatMap(Object.keys))];

console.log(keys);

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

1 Comment

sry i delete my comment, i wanted to say he should write Object.keys(arr[0]) instead of Object.keys(arr)[0] (so we agree :) )
1

This should work.

arr = [
   {id: 1, desc: "", name: "", objectives: Array(3)},
   {id: 2, desc: "", name: "", objectives: Array(3)},
   {id: 3, desc: "", name: "", objectives: Array(3)},
   {id: 4, desc: "", name: "", objectives: Array(3)},
];

keyArr = [];
Object.keys(arr).find(keys => {
    Object.keys(arr[keys]).find(key => {
    keyArr.push(key);
  });
})
console.log(keyArr);

Comments

0
    const arr =[
      {id: 1, desc: "", name: "", objectives: Array(3), …},
      {id: 2, desc: "", name: "", objectives: Array(3), …},
      {id: 3, desc: "", name: "", objectives: Array(3), …},
      {id: 4, desc: "", name: "", objectives: Array(3), …},
    ];

    let distinct = function(arr, key_name) {
       let seen_values = [];
       let unique_objects = [];
       for(i = 0; i < arr.length; i++) {
         // If this value is new, a.k.a. distinct then add it to our filtered list.
         if(!seen_values.includes(arr[i][key_name]) {
            unique_objects.push(arr[i])
         }
         // Add this value to our seen values.
         seen_values.push(arr[i][key_name])
       }
       return unique_objects;
    }

    // Example usage, filter by desc
    let filtered_set = distinct(arr, "desc")

    // Example usage, filter by id
    let filtered_set_2 = distinct(arr, "id")

So this should work. The distinct function will filter the passed in arr by the given key_name. One limitation of this approach is that if one of the values in your object is yet another object you will need to do deeper inspection of that object to be able to compare them.

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.