Reputation: 15589
I have an Object
obj
as below:
const obj = {
Hostname: "abc"
id: 189883
message: "error message"
name: "script name"
};
Hostname
and it value.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
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
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:
Object.keys()
to filterObject.values()
array.map()
array.filter()
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
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
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
Reputation: 866
You can use delete
and Object.values
.
delete obj["Hostname"];
Object.values(obj).join(":");
Upvotes: 0
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