Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to get numbers out of a JavaScript object? [duplicate]

data = [ RowDataPacket { test: '12312311' },
    RowDataPacket { test: '12312312' },
    RowDataPacket { test: '12312313' } ]

I would like to get the numbers from this object into an array in Javascript. I already tried it with .split(/(\d+)/); but I get this error in Chrome:

Uncaught TypeError: data.split is not a function.

How can I extract the numbers?

like image 387
Suppenterrine Avatar asked Jan 01 '23 13:01

Suppenterrine


1 Answers

Your attempt will not work because .split() is a string method - it's used for breaking up a string into an array. You already have an array. JSON seems to be irrelevant here, as what you've shared is not JSON.

You can use Array.map() to mutate each member of an array. You want to get each member's test value, and parse it.

var data = [ { test: '12312311' }, { test: '12312312' }, { test: '12312313' } ];
var result = data.map(obj => parseInt(obj.test));
console.log(result);
like image 187
Tyler Roper Avatar answered Jan 04 '23 03:01

Tyler Roper