Using Lodash, I am trying to extract values from an object while excluding specific fields. Consider the object below:
var x = {
id: 0, // <-- id is excluded from output
a: 1,
b: 2,
c: 3
};
In real-world scenarios, objects are typically small with an id
field and varying other fields.
I aim to create a pipe-delimited string containing the values in this object (in any order), but excluding certain fields. The code snippet below accomplishes this task:
var keys_i_want = _
.chain(x)
.keys()
.filter(x=> x !== "id")
.value();
var result = _.values(_.pick(x, keys_i_want)).join("|"); // --> '1|2|3'
The expected outcome for this example is 1|2|3
.
I am interested in a more concise approach to achieve this functionality using Lodash. Is there a simpler and direct way without the need for extensive code like above?
Can the step where we select out the values be integrated into the original chain
expression? Is there a clearer and more efficient method for this operation?