1

Is there anyway to convert this like the example below?

Convert this:

[
  RowDataPacket { title: 'Code' },
  RowDataPacket { title: 'Pizza' }
]

Into this:

['Code', 'Pizza']
4
  • That's not even an valid array. Commented Mar 28, 2017 at 13:45
  • @abhishekkannojia Whoops forgot the ' Commented Mar 28, 2017 at 13:47
  • 2
    by using Array#map: yourArray.map(rdp => rdp.title). Check this out: stackoverflow.com/a/31229034/6567275 Commented Mar 28, 2017 at 13:47
  • @Thomas I feel stupid, thanks a lot. Commented Mar 28, 2017 at 13:50

2 Answers 2

4

I've provided you two possible solutions, in case if it's just an array of objects or an array of nested objects.

var arr = [{RowDataPacket: { title: 'Code' }}, {RowDataPacket: { title: 'Pizza' }}],
    res = arr.map(v => v.RowDataPacket.title);
    console.log(res);

var arr = [{ title: 'Code' }, { title: 'Pizza' }],
    res = arr.map(v => v.title);
    console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

-1

Create a new array

var newArr = [];

And then push each item into the array

result.forEach(function(obj){
   newArr.push(obj.title);
}

1 Comment

foreach should be forEach() - but I can't make such a short edit :)

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.