Convert array of objects to array of specific property from each object

Karma

I am using felixge/node-mysql to run this query: "SELECT DISTINCT tablename.id FROM tablename"

The rows returned are (obviously) in this format: [{id: 123},{id: 234},{id: 345}]

I can't figure out how to get this: [123, 234, 345]

user2864740

With ES5, such is rather easy using a map transformation:

The map() method creates a new array with the results of calling a provided function on every element in this array.

For example:

var arr = [{id: 123},{id: 234},{id: 345}];
var res = arr.map(function (o) {
   return o.id;
});

Doing this "manually" with a loop would look like so:

var arr = [{id: 123},{id: 234},{id: 345}];
var res = [];
// Loop over the array
for (var i = 0; i < arr.length; i++) {
  var item = arr[i];
  // And add new value we want to result array
  res.push(item.id);
}

(Note that the variables are function-scoped, but I prefer to use "close placement" of var.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove a specific Object from an array of Objects, by object's property?

From Dev

convert specific properties of object to an array of objects

From Dev

Select a specific property from array of objects angular

From Dev

Convert array of objects into object

From Dev

PHP Get a specific property from an array of objects with different array count

From Dev

Convert array of objects to object of objects

From Dev

Joi validate array of objects, such that only one object has specific property

From Dev

Joi validate array of objects, such that only one object has specific property

From Dev

Angular expression get property of object from equal property in array of objects

From Dev

Convert array of objects into array of primitives (extracted from object properties)

From Dev

Convert array of objects into array of primitives (extracted from object properties)

From Dev

Convert array of objects to an object of arrays

From Dev

Convert file object to array of objects

From Dev

How to convert object into array of objects

From Dev

How to access an object property from an array of objects in a click handler?

From Dev

Create dictionary from an array of objects using property of object as key for the dictionary?

From Dev

How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

From Dev

Javascript Filter Objects in Array and return Property of Object in Array of Object in Array

From Java

Get specific object by id from array of objects in AngularJS

From Dev

ReactJS: Update a specific Object from an array of Objects in the state

From Dev

how to convert object into array of objects (or collection of objects)

From Dev

How to convert object containing Objects into array of objects

From Dev

NodeJS - Convert Array of Objects into Object of Objects

From Dev

convert an array of objects to one object of objects

From Dev

Looping over objects in array and adding property to each

From Dev

Convert an array of objects into a nested array of objects based on string property?

From Java

From an array of objects, extract value of a property as array

From Dev

Display a specific property from an array

From Dev

Sort array objects by property value in specific order

Related Related

  1. 1

    How to remove a specific Object from an array of Objects, by object's property?

  2. 2

    convert specific properties of object to an array of objects

  3. 3

    Select a specific property from array of objects angular

  4. 4

    Convert array of objects into object

  5. 5

    PHP Get a specific property from an array of objects with different array count

  6. 6

    Convert array of objects to object of objects

  7. 7

    Joi validate array of objects, such that only one object has specific property

  8. 8

    Joi validate array of objects, such that only one object has specific property

  9. 9

    Angular expression get property of object from equal property in array of objects

  10. 10

    Convert array of objects into array of primitives (extracted from object properties)

  11. 11

    Convert array of objects into array of primitives (extracted from object properties)

  12. 12

    Convert array of objects to an object of arrays

  13. 13

    Convert file object to array of objects

  14. 14

    How to convert object into array of objects

  15. 15

    How to access an object property from an array of objects in a click handler?

  16. 16

    Create dictionary from an array of objects using property of object as key for the dictionary?

  17. 17

    How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

  18. 18

    Javascript Filter Objects in Array and return Property of Object in Array of Object in Array

  19. 19

    Get specific object by id from array of objects in AngularJS

  20. 20

    ReactJS: Update a specific Object from an array of Objects in the state

  21. 21

    how to convert object into array of objects (or collection of objects)

  22. 22

    How to convert object containing Objects into array of objects

  23. 23

    NodeJS - Convert Array of Objects into Object of Objects

  24. 24

    convert an array of objects to one object of objects

  25. 25

    Looping over objects in array and adding property to each

  26. 26

    Convert an array of objects into a nested array of objects based on string property?

  27. 27

    From an array of objects, extract value of a property as array

  28. 28

    Display a specific property from an array

  29. 29

    Sort array objects by property value in specific order

HotTag

Archive