Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Join elements of an object and exclude some

I have a Typescript project where I want to join all the values of an Object except one.

This is my Object:

let dataInit = {
  "host": "CAPR",
  "ua": "RMA",
  "country": "VE",
  "page":3
};

This is what I do:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let dataJoin = Object.values(dataInit).join(',')

This is what I get:

CAPR,RMA,VE,3

I need to know how to remove the ‘page’ property, this is what I want:

CAPR,RMA,VE

>Solution :

If you don’t know the other attributes, you can first use Object.entries() to give you an array of arrays containing the keys and values. That array can then be filtered to remove the "page" element, mapped to just contain the value, and finally joined.

let dataInit = {
  "host": "CAPR",
  "ua": "RMA",
  "country": "VE",
  "page":3
};

console.log(
Object.entries(dataInit)
  .filter(([key,val]) => key !== "page")
  .map(([_,val]) => val)
  .join(",")
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading