Is there a way to extract just the properties on an array?

Viewed 62

enter image description here

I have an array that has properties attached to it like this.

Is there a way to extract just the properties (by filtering out all the array related data) and turning this into a separate object with just the properties?

2 Answers

You can filter the entries of the object and remove all numeric keys.

let o = [1,2,3,4];
o.x = 2;
o.y = 3;
o.z = 'test';
const res = Object.fromEntries(Object.entries(o).filter(([k])=>isNaN(k)));
console.log(res);

To get the whole keys of the array, you can use the command Object.keys(arrayName);

To get the keys that is not a number and create a separate object of them and their values you can do following:

let objectName = {};
let arrayKeys = Object.keys(arrayName);
for (let i = 0; i < arrayKeys.length; i++) {
   if(isNaN(arrayKeys[i]) === true) {
      objectName['i'] = arrayName[i];
   }
}

Now you have an object with the name objectName and it contains all properties of your array which I named it arrayName and these properties are not numbers

Related