0

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:

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
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
stark
  • 59
  • 1
  • 7
  • Does this answer your question? [how to remove json object key and value.?](https://stackoverflow.com/questions/46599519/how-to-remove-json-object-key-and-value) – Alan Yu Jan 31 '22 at 09:17
  • Welcome to Stack Overflow! Please take the [tour] if you haven't already (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO and elsewhere, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mre] showing your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Jan 31 '22 at 09:17
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jan 31 '22 at 09:18
  • `Object.values()` returns an array, so... `Array.prototype.filter()` – Andreas Jan 31 '22 at 09:19
  • Also, beware that although JavaScript object properties do have an order, it's not one you should typically rely on as it is more complicated than it seems and depends (in large part) on the way the object was created. So depending on how the object was creaetd, you might get `CAPR,RMA,VE` or `RMA,CAPR,VE` or any of seven other combinations. Instead, pick the properties you want explicitly and build a string from them in the order that you need. – T.J. Crowder Jan 31 '22 at 09:19
  • You could create a function like this instead of relying on the order of keys or the extra keys in the object: `const commaSeperated = ({ host, ua, country }) => [host, ua, country].join(',')` – adiga Jan 31 '22 at 09:22
  • It worth removing `page` property before iteration through all keys. See [example](https://tsplay.dev/NDPRjN) – captain-yossarian from Ukraine Jan 31 '22 at 09:31

4 Answers4

2

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(",")
)
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
1

I would destructure the object first and create a new one

const { host, ua, country } = dataInit
const dataNew = { host, ua, country }

And then call the values join method on the new object.

0

You could filter the array resulted:

let dataJoin = Object.values(dataInit).filter(element => typeof element === "string").join(",");

Or you can use destructuring as presented in the other comments.

pustiul500
  • 55
  • 11
  • 1
    Doing it in a single line is fine but in this case the ones that are string are taken, I think it is more reliable to take all except the 'page' property – stark Jan 31 '22 at 09:39
  • But what if more properties come in the future? I mean, this is a subjective one. If all the properties he wants are strings, my way could work. If not, something as extracting specific properties is a better way. – pustiul500 Jan 31 '22 at 16:08
0

You can use for...in loop to iterate over element and skip page property.

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

const convertObject = (obj) => {
  let list = [];
  for (const prop in obj) {
    if (prop !== "page") {
      list.push(obj[prop]);
    }
  }
  return list.join(",");
}

console.log(convertObject(dataInit));
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22