0

So I am running a NodeJS Serverless instance, using Knex.js as my DB middleware. When attempting to return JUST the value from a request to Knex, I always get [ { vrn: 'xx12xyz' } ]. I have tried using Object(), does anyone have a quick one liner or small helper function that can help me flatten this into a direct array such that I can pick out JUST the value?

This is the code I am using:

const vrnList = await db('vehicles').select('vrn').where('id', '=', '1');
    console.log("The quick car with the VRN ", Object.values(vrnList), " overtook me on the freeway");

TL;DR: Splitting the object and array id from the values

0

2 Answers 2

1

Try this one:

let onlyValues = vrnList.map(result => result.vrn);
Sign up to request clarification or add additional context in comments.

Comments

1

Since the OP seems to retrieve the value(s) key agnostic via Object.values which returns an array of values, the OP in addition needs to utilize flatMap ...

vrnList.flatMap(item => Object.values(item))

console.log(
  [ { vrn: 'xx12xyz' } ].flatMap(item => Object.values(item))
);
console.log(
  [
    { vrn: 'xx12xyz' },
    { vrn: 'xx98abc' },
    { vrn: 'yy34xyz' },
  ].flatMap(item => Object.values(item))
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

4 Comments

All fixed - Thank you. I was left with an array but using the toString() function fixed it. Thanks!
@JPA ... maybe the OP considers giving an own answer of how the problem got solved. I personally doubt that a toString on the provided data structure actually was the only thing needed in order to fix all issues.
No what I meant was that your answer, partnered with toString(), made it work
@JPA ... "No what I meant was that your answer, partnered with toString(), made it work" ... good to hear. And at SO it is considered to be a nice gesture from the one who got help, to accept the answer which was the most helpful in solving the OP's problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.