6

I am trying to create an array which will hold just the Name elements from this array:

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}];

I am new to JavaScript and I am not sure how this would be done.

3
  • 3
    To get first element from array, use var first = array[0]; To get Name from it, use first.Name. Commented Feb 24, 2016 at 14:58
  • 1
    FWIW the array you're reading information from to get the Name elements looks badly designed to start with - it looks like it ought to be an array of objects that looks more like: [{Name: "steve", Age: 18, Location: "Uk"}] rather than individual objects for each property. Commented Feb 24, 2016 at 14:59
  • 2
    So the real question is "How can I extract a particular object from an array based on the value of one of its properties?" right? Commented Feb 24, 2016 at 14:59

4 Answers 4

7

Here is a good read to understand how object works: http://www.w3schools.com/js/js_objects.asp

if you really want the name first element from this array just use

array[0]
Sign up to request clarification or add additional context in comments.

Comments

4

If you want an array of just the objects that have a Name key, you can use Array.prototype.filter().

This will return a two-item array [{Name: "steve"}, {Name: "conor"}]:

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}, 
             {Name: "conor"}, {Age: 18}, {Location: "Uk"}];

var names = array.filter(function(obj) {
  if ('Name' in obj) {
    return true;
  } else {
    return false;
  }
});

If you want an array of just the Name values of just the objects that have a Name key, you can use Array.prototype.filter() and Array.prototype.map() together.

This will return a two-item array ["steve", "conor"]:

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}, 
             {Name: "conor"}, {Age: 18}, {Location: "Uk"}];

var names = array.filter(function(obj) {
  if ('Name' in obj) {
    return true;
  } else {
    return false;
  }
}).map(function(obj) { return obj['Name']; });

Either way, you may want to take another look at the structure of your array. It probably makes more sense to group your "people" so that each one is a single object, something like:

[{name: "steve", age: 18, location: "Uk"}, {name: "conor", age: 18, location: "Uk"}]

1 Comment

In this case you have two array object, I only want to get in first name?Its steve
3

A new feature that javascript has is array destructuring!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

It removes all the complexity of using a custom function, or using the hard coded 0 index (seems like a code smell).

Just destructure the value into a variable. If the array is empty the value will be undefined.

const arr = ["one", "two", "three"];
const [first] = arr;

Comments

2

I know this is an older post but I have a pretty cool solution that gets key from the array and then uses that to return the first item, or alternatively last, the current code is es6 but its simple to convert to es5.

Array.prototype.first = function () { let k = Object.keys( this ); return this[ k[ 0 ] ]; }
//re for last...
Array.prototype.last = function () { let k = Object.keys( this ); return this[ k[ k.length - 1 ] ]; }

To change to es5 use var rather than let.

Reason for this prototype is to make sure that regardless or the index system used will always return first element or the last element...

Comments

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.