meallhour
meallhour

Reputation: 15589

How to exclude keys from a map and display only the values?

I have an Object obj as below:

const obj = {
         Hostname: "abc"
         id: 189883
         message: "error message"
         name: "script name"
};
  1. I want to exclude the key Hostname and it value.
  2. I only want to capture the values excluding keys such that the output looks as below: 189883:error message:script name

The final output should be a string. I have written the following code but it is creating array element for every letter

const resultObj = Object.values(vascoResponse.rows[0]obj).map((row) =>
  Object.values(row).filter((val) => val)
);

Upvotes: 0

Views: 2364

Answers (6)

Mahdi Rezazadeh
Mahdi Rezazadeh

Reputation: 536

You could use this:

Object.fromEntries(Object.entries(obj).filter(([key, value]) => key !== 'id' && key !== 'Hostname'))

let obj = {
  Hostname: "abc",
  id: 189883,
  message: "error message",
  name: "script name"
};

obj = Object.fromEntries(Object.entries(obj).filter(([key, value]) => key !== 'id' && key !== 'Hostname'))

console.log(obj)

Upvotes: 0

Zachary Fejes
Zachary Fejes

Reputation: 58

So I see where you're going with this, but I'd personally avoid Object.values() altogether. As a general rule when deciding what to do with an array, I try to ask these questions:

  • Do I want to extract a subset of values from an object based on keys? -> use Object.keys() to filter
  • Do I want to extract a subset of values from an object based on values? -> use Object.values()
  • Do I want to turn one array into another of equal length? -> use array.map()
  • Do I want to turn one array into a shorter array? -> use array.filter()
  • Do I want to turn an array into a single value like a string? -> use array.reduce()

Since you want a subset of values from the Object, based on the keys and you want to turn it into a string, then we should probably use Object.keys(), array.filter(), and then array.reduce() like this:

let obj = {
   Hostname: "abc",
   id: 189883,
   message: "error message",
   name: "script name"
};

let resultString = Object.keys(obj) //gets an array of keys from the object
  .filter(key => key != "Hostname") //removes 'Hostname' from the array
  .reduce((accumulator, key) => 
    accumulator == "" ? accumulator + obj[key] : accumulator + ":" + obj[key],
   "");

console.log(resultString);

If you run that code snippit you'll find that it outputs a string exactly like you were hoping for every time. The ternary operator inside the reducer is just a sneaky way of adding the ":" colon character before every new value unless it's the first one.

Upvotes: 2

Nick Parsons
Nick Parsons

Reputation: 50854

You could grab the entries of your object to obtain an array of [[key, value], [key2, value2], ...] pairs. Then you can use .filter() on it to remove the pair with a key of "Hostname". Lastly, you can map each inner array to its value and use .join(":") to obtain a string:

const obj = {
   Hostname: "abc",
   id: 189883,
   message: "error message",
   name: "script name"
};

const res = Object.entries(obj)
             .filter(([key]) => key !== "Hostname")
             .map(([,v]) => v).join(":");

console.log(res);

If you need this functionality to be extracted into a function, you could destructure the object argument to obtain all keys/value excluding the Hostname, and then grab and join the values based on that:

const obj = {
   Hostname: "abc",
   id: 189883,
   message: "error message",
   name: "script name"
};

const getString = ({Hostname, ...r}) => Object.values(r).join(':');
console.log(getString(obj));

Upvotes: 4

Sven.hig
Sven.hig

Reputation: 4519

Use Object.values()

const objec = {
  Hostname: "abc",
  id: 189883,
  message: "error message",
  name: "script name"
};
res = Object.values(objec).filter((o,i) => o != "abc" ).join(":")
console.log(res)

Upvotes: 1

NeK
NeK

Reputation: 866

You can use delete and Object.values.

delete obj["Hostname"];
Object.values(obj).join(":");

Upvotes: 0

Carsten Massmann
Carsten Massmann

Reputation: 28206

const obj = {
     Hostname: "abc",
     id: 189883,
     message: "error message",
     name: "script name"
};

let str=Object.entries(obj)
 .filter(([k])=>k!=="Hostname")
 .map(([k,v])=>v).join(":");
console.log(str)

Upvotes: 1

Related Questions