Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning only certain properties from an array of objects in Javascript [duplicate]

If I have an object such that

var object = function(key,text)
{
    this.key = key;
    this.text = text;
}

And create an array of these objects

var objArray = [];
objArray[0] = new object('key1','blank');
objArray[1] = new object('key2','exampletext');
objArray[2] = new object('key3','moretext');

is there a way that I can retrieve only one of the properties of all of the objects in the array? For example:

var keyArray = objArray["key"]; 

The above example doesn't return set keyArray to anything, but I was hoping it would be set to something like this:

keyArray = [
    'key1',
    'key2',
    'key3']

Does anyone know of a way to do this without iterating through the objArray and manually copying each key property to the key array?

like image 342
ragesalmon Avatar asked Jun 26 '14 21:06

ragesalmon


People also ask

How to get only specific values in an array of objects?

Let’s say the following is our array of objects − To get only specific values in an array of objects in JavaScript, use the concept of filter (). node fileName.js.

How to get multiple properties of an array of objects?

6. For in loop to access property of array Another way to get mutiple properties of an array of objects by using for-in loop iterate over the key values of each object in an array of object and push the key-value pair in an array and finally display propety.

How to access a property of an object in JavaScript ES6?

The Javascript ES6 array.from () is a static method that creates an array object from arraylike or iterable objects (string, array, map, set). We can access a property of an object by using an array.from (), in this below example we are accessing the Empname from the employee object. 5. Reduce () to access property of object

What are the basic functions of array in JavaScript?

You may remember the function Array.includes which is similar to Array.some, but works only for primitive types. In this article, we went through the basic functions that help you create, manipulate, transform, and loop through arrays of objects. They should cover most cases you will stumble upon.


2 Answers

This is easily done with the Array.prototype.map() function:

var keyArray = objArray.map(function(item) { return item["key"]; });

If you are going to do this often, you could write a function that abstracts away the map:

function pluck(array, key) {
  return array.map(function(item) { return item[key]; });
}

In fact, the Underscore library has a built-in function called pluck that does exactly that.

like image 183
Jacob Krall Avatar answered Oct 21 '22 22:10

Jacob Krall


var object = function(key,text) {
    this.key = key;
    this.text = text;
}

var objArray = [];
objArray[0] = new object('key1','blank');
objArray[1] = new object('key2','exampletext');
objArray[2] = new object('key3','moretext');

var keys = objArray.map(function(o,i) {
  return o.key;
});

console.log(keys); // ["key1", "key2", "key3"]

JS Bin Example

http://jsbin.com/vamey/1/edit

Note that older browsers may not support map but you can easily do this with a for loop:

var keys = [];

for (var i = 0; i < objArray.length; i++) {
  keys.push(objArray[i].key);
}

JS Bin Example

http://jsbin.com/redis/1/edit

like image 4
Miguel Mota Avatar answered Oct 21 '22 21:10

Miguel Mota