Home > Enterprise >  Const array using data from two objects in TypeScript
Const array using data from two objects in TypeScript

Time:09-28

I require some help on this particular issue I'm encountering, I'm trying to const an array using two objects with Object.keys(...).map and &&.

Code:

const plugins: PluginManifest[] = Object.values(window.Aliucord.pluginManager.plugins).map((p) => p.manifest) && Object.values(window.Aliucord.pluginManager.disabledPlugins).map(p => p);

The above code uses the data only from the disabledPlugins Object.

Am I doing something wrong or is there a better way to do this?

CodePudding user response:

In this case I would expect && to simply be resolving to the second part of its expression. && certainly doesn't combine arrays. But you can combine them into one array by spreading them into an array literal. For example:

const firstArray = [1,2,3];
const secondArray = [4,5,6];
const finalArray = [...firstArray, ...secondArray];
console.log(finalArray);

So in your case, since those .map() operations produce arrays, you can spread them into another array:

const plugins: PluginManifest[] = [
  ...Object.values(window.Aliucord.pluginManager.plugins).map((p) => p.manifest),
  ...Object.values(window.Aliucord.pluginManager.disabledPlugins).map(p => p)
];
  • Related