Retrieve all the keys from an array of objects that contain values in the form of arrays

Looking for an efficient way to extract all keys from an array of objects whose values are arrays, without any duplicates. I want to achieve this using pure JavaScript, without relying on libraries like lodash or underscore. Any suggestions on how to improve my current solution would be greatly appreciated. The keys within the objects are dynamic and not always consistent.

The desired output based on my example should be: [ "stuff", "type", "misc", "something" ]

const items = [{
    name: "Joe",
    occupied: "no",
    mobile: "yes",
    treatment: "no",
    date: "29-03-2020",
    age: "15",
    stuff: ["A", "B", "C"],
    type: ["1", "2"]
  },
  {
    name: "Jack",
    occupied: "yes",
    mobile: "no",
    treatment: "no",
    date: "02-03-2020",
    age: "20",
    stuff: ["A", "B", "C", "D", "E"],
    type: ["8", "6"],
    misc: ["otherStuff", "someStuff"]
  },
  {
    name: "Jane",
    occupied: "no",
    mobile: "yes",
    treatment: "yes",
    date: "15-02-2020",
    age: "28",
    stuff: ["C", "D", "E"],
    type: ["4", "7"],
    something: ["xxx", "ccc"]
  }
];
function getKeysWithArrayValues(myArray) {
  const result = [];
  myArray.forEach(item => Object.entries(item).forEach(itm => itm.filter(Array.isArray).forEach(x => result.push(itm.slice(0, 1))));
  
  return flatArray(result)
};

function flatArray(array) {
  return array.reduce((acc, val) => Array.isArray(val) ? acc.concat(flatArray(val)) : acc.concat(val), []);
};
const ttt = getKeysWithArrayValues(items);
const flat = Array.from(new Set(ttt))
console.log(flat);

Answer №1

Here is a possible solution that may assist you. The algorithm outlined here is quite straightforward, and I believe there is no necessity for a lengthy explanation. Essentially, we iterate through an array using the reduce method and construct a new array consisting only of keys that meet our specified criteria.

The check Array.isArray(rec[key]) verifies whether the value is an array.

The condition acc.indexOf(key) < 0 confirms if the key has not already been added to the resulting array in any previous steps.

const modifiedArray = items
  .reduce((acc, rec) => {
    return [...acc, ...Object.keys(rec).filter(key => Array.isArray(rec[key]) && acc.indexOf(key) < 0)]
  }, [])

Answer №2

If you want to extract unique keys from objects, one approach is to utilize a Set.

const
    data = [{ name: "Alice", age: 25, city: "New York" }, { name: "Bob", age: 30, city: "Los Angeles" }, { name: "Charlie", age: 35, city: "Chicago" }],
    uniqueKeys = Array.from(
         new Set(data.flatMap(obj => Object.keys(obj)))
    );

console.log(uniqueKeys);

Answer №3

Utilize the flatMap method in order to loop through each object's keys and generate an array of keys that meet the condition of containing an array as its value:

const items=[{name:"Joe",occupied:"no",mobile:"yes",treatment:"no",date:"29-03-2020",age:"15",stuff:["A","B","C"],type:["1","2"]},{name:"Jack",occupied:"yes",mobile:"no",treatment:"no",date:"02-03-2020",age:"20",stuff:["A","B","C","D","E"],type:["8","6"],misc:["otherStuff","someStuff"]},{name:"Jane",occupied:"no",mobile:"yes",treatment:"yes",date:"15-02-2020",age:"28",stuff:["C","D","E"],type:["4","7"],something:["xxx","ccc"]}];
  
const keysWithArrays = new Set(
  items.flatMap(
    item => Object.keys(item).filter(key => Array.isArray(item[key]))
  )
);
console.log([...keysWithArrays]);

Answer №4

To determine if a value is an array, you can utilize the set method along with Array.isArray().

let myArray = new Array();    const elements = [{name: "Joe",occupied: "no",mobile: "yes",treatment: "no",date: "29-03-2020",age: "15",stuff: ["A", "B", "C"],type: ["1", "2"]},{name: "Jack",occupied: "yes",mobile: "no",treatment: "no",date: "02-03-2020",age: "20",stuff: ["A", "B", "C", "D", "E"],type: ["8", "6"],misc: ["otherStuff", "someStuff"]
}, {name: "Jane",occupied: "no",mobile: "yes",treatment: "yes",date: "15-02-2020",age: "28",stuff: ["C", "D", "E"],type: ["4", "7"],something: ["xxx", "ccc"]}];

elements.forEach((object)=> Object.keys(object).forEach( property=>{if(Array.isArray(object[property])) 
myArray.add(property)})); console.log(Array.from(myArray));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Answer №5

Here is an alternative method using Object.fromEntries, flatMap, and filter.

const items = [
  {
    name: "Joe",
    occupied: "no",
    mobile: "yes",
    treatment: "no",
    date: "29-03-2020",
    age: "15",
    stuff: ["A", "B", "C"],
    type: ["1", "2"]
  },
  {
    name: "Jack",
    occupied: "yes",
    mobile: "no",
    treatment: "no",
    date: "02-03-2020",
    age: "20",
    stuff: ["A", "B", "C", "D", "E"],
    type: ["8", "6"],
    misc: ["otherStuff", "someStuff"]
  },
  {
    name: "Jane",
    occupied: "no",
    mobile: "yes",
    treatment: "yes",
    date: "15-02-2020",
    age: "28",
    stuff: ["C", "D", "E"],
    type: ["4", "7"],
    something: ["xxx", "ccc"]
  }
];

const keys = Object.keys(
  Object.fromEntries(
    items.flatMap(item =>
      Object.entries(item).filter(([, value]) => Array.isArray(value))
    )
  )
);

console.log(keys);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve JSON data from a WordPress site that is powered by WooCommerce, using basic authentication

I am in the process of creating an Android application that retrieves product data from a WordPress site. The website has the Woocommerce plugin installed. I have been able to successfully obtain the JSON data using Postman software and basic authenticat ...

support for fullscreenchange event across different browsers

I have been exploring how to add an event listener for fullscreen change in my Next.js app, and I noticed that many example codes use the webkit, moz, and ms prefixes together. However, after testing on Edge, Chrome, and Firefox, it seems that using only t ...

retaining the scroll position of a window when refreshing a div

There's this issue that's been bothering me lately. I update the database via an ajax call, refresh the div, everything works fine, but it still scrolls to the top of the page. Here's what I have attempted: function postdislike(pid, user, ...

Making AJAX requests with Laravel using the XMLHttpRequest object is a

When using Laravel to upload a file and create a progress bar with ajax requests, the form action routes to the controller in this way: <form action="{{ URL::route('upload-file-form-post') }}" method="POST" enctype="multipart/form-data"> ...

Eliminate any spaces from the JSON string's "outside" keys and values

I have a JSON string saved in a database field that looks like this: {"name" : "John Paul Mark", "surname" : "Johnson"} It's important to note that the name consists of three separate names with spaces between them. I am looking to remove the space ...

The email protected function is failing to display components and is not generating any error logs

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05776064667128776a7071607728616a6845332b31">[email protected]</a> seems to be having trouble rendering components. I've been away from React for a bit and now I ...

Issue with setting column width using JSON in Modern SharePoint Online Lists is not functioning as expected

Although the script seems to be functioning well with no errors and a noticeable change in text color, I have been experiencing an issue where adjusting the width value does not have any effect. Regardless of whether I input 300, 600, or 1000 as the width, ...

updateStatusCallback function is not defined in the Facebook example using jQuery

I've been trying to incorporate Facebook integration into my HTML code, specifically adding features like Facebook login and sharing functionalities. However, I've hit a roadblock in the process. Even after searching extensively for solutions, I ...

svg-to-json.js | The mysterious disappearing act of my file

After completing the installation process for svg-to-json.js as detailed in my previous post, I ran the command: node svg-to-json.js filename.txt The expectation was that a .json file would be generated, but I couldn't locate it. Is it supposed to ...

What is preventing me from accessing session data using nuxtServerInit?

When attempting to retrieve sessions, I encounter an issue. Is everything set up correctly in the following main files: server, config? store/index.js export const actions = { nuxtServerInit ({ commit }, { req }) { console.log(req); } The console log ...

"Encountering issues with Rails and AJAX where the data returning is showing up

I am facing a challenge while trying to use AJAX in Rails to POST a comment without using remote: true. I am confused as to why my myJSON variable is showing up as undefined, while data is returning as expected. Check out my code below: function submitVi ...

Is there a way to capture all ajax responses?

Is it possible to capture all responses from an ajax request, regardless of the library being used such as jQuery, prototype, or just the vanilla XMLHttpRequest object? I am looking for a way to append to any existing handler without removing it. Thank y ...

How to create a sequence of queries to a mongoDB database (using mongoose) depending on a condition

Source Code let placeForSearch="hampi" let filteredHotelFacilities=req.body.filter //["Free Wifi"] let hotels = await Hotel.find({placeForSearch}) .where("facilities") .select(selectedProperties) .skip(pageNu ...

What is the proper approach for returning JSON in a Rails GET request?

As an illustration when making a request GET --> http:://{url}?search={username} I must provide: [ {id: 1, name: 'sande', nickname: 'username'}, {id: 2, name: 'sande2', nickname: 'username'} ] This is how ...

Is there a method to verify the consumability of an API without having to make a direct request to it?

I'm working on an idle timer feature where the API will be called to update data once the timer runs out. How can I check if the API is still usable without actually sending a request? ...

One way to filter using a single array

Currently testing some angularJS with this particular example http://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_filter <ul> <li ng-repeat="x in names | filter : 'i'"> {{ x }} </li> </ul> Is ther ...

Using TypeOrm QueryBuilder to establish multiple relations with a single table

Thank you for taking the time to read and offer your assistance! I am facing a specific issue with my "Offer" entity where it has multiple relations to "User". The code snippet below illustrates these relationships: @ManyToOne(() => User, (user) => ...

The onInvoke hook in Zone.js is receiving an inaccurate currentZone value

Greetings. In the comments for the ZoneSpec interface (found in zone.ts file), it states that the onInvoke hook must receive currentZone as the second parameter. If creating an interceptor zone, the reference to that zone should be passed as the second pa ...

Guide on positioning components to the right side of the NavigationDrawer in Vuetify with VueRouter

Working on my VueJS project, I've implemented a system to display content based on the user login using Firebase and Vuex state management. When a user is logged in, the content is shown using the v-if directive. Currently, I have successfully placed ...

A Promise-based value returned by a Typescript decorator with universal methods

I am currently working on creating a method decorator that can be applied to both prototype and instance methods. Referenced from: Typescript decorators not working with arrow functions In the code provided below, the instanceMethod() is returning a Prom ...