How to get an item from javascript array

Sorry maybe very easy for someone, but how can I get the value from this array?

        this.optionSelect = [{value: 'aa', name: 'ccc'}];

I tried :

console.log( this.optionSelect['value']); 

also `

console.log( this.optionSelect[0]);`

You can reach this value by the following. Arrays are Indexed, the 0 is which index in the array you are accessing so. JavaScript Arrays.

this.optionSelect[0] will be -> {value: 'aa', name: 'ccc'};

Then add .value as the key you want to access of the object. Property access.


JavaScript Array find() Method, Definition and Usage. The find() method returns the value of the first element in an array that pass a test (provided as a function). The find() method executes the � How about using _.find(collection, [predicate=_.identity], [fromIndex=0]) of lo-dash to get object from array of objects by object property value. You could do something like this: var o = _.find(jsObjects, {'b': 6}); Arguments: collection (Array|Object): The collection to inspect. [predicate=_.identity] (Function): The function invoked per iteration.

They are multiple level to need to interact with.

First level is the array. You can see it's an array because of the delimiting characters [ ] :

[{value: 'aa', name: 'ccc'}];

You access the first element of the array using an index, like : this.optionSelect[0]

Then you have to deal with an object. You can see it's an object because of the delimiting characters { } :

{value: 'aa', name: 'ccc'}

To access an object, you have to use the name of the key you want, like : obj.value or obj['value'].

Both notation works.

Now do both in the same line :


Array.prototype.find(), Find the value of the first element/object in the array, otherwise undefined is returned. var result = jsObjects.find(obj => { return obj.b === 6 }). See the MDN Docs� The indexOf() method searches the array for the specified item, and returns its position. The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array. Returns -1 if the item is not found.

Get the value of the first item in the array:

const value = this.optionSelect[0].value;

Also try destructuring:

const [{ value }] = this.optionSelect;

Array, The arr.find() method is used to get the value of the first element in the array that satisfies the provided condition. It checks all the elements of the� Get the first and last item in an array using JavaScript Last Updated: 23-04-2019. Javascript array is a variable which holds multiple values at a time. The first and

Get JavaScript object from array of objects by value of property , arrays also have a special properties lenght and many function that can manipulate the elements. Declaring an array. Javascript's elegance owes a lot to its� function random_item(items) { return items[Math.floor(Math.random()*items.length)]; } const items = [254, 45, 212, 365, 2543]; console.log(random_item(items)); Live Demo: See the Pen JavaScript - Get a random item from an array - array-ex- 35 by w3resource (@w3resource) on CodePen.

JavaScript Array find() Method, To make things easier, in this chapter they are split into groups. let result = arr. find(function(item, index, array) { // if true is returned, item is� JavaScript automatically converts an array to a comma separated string when a primitive value is expected. This is always the case when you try to output an array. These two examples will produce the same result:

JavaScript Arrays - tips, tricks and examples, The JavaScript Array.find method is a convenient way to find and return the first occurence of an element in an array, under a defined testing function. When you � JavaScript Array elements can be removed from the end of an array by setting the length property to a value less than the current value. Any element whose index is greater than or equal to the new length will be removed.

  • Try this.optionSelect[0].value
  • Possible duplicate of How can I access and process nested objects and arrays?