How to get value in an object array by key

javascript object get value by key
javascript get value by key in array of objects
javascript find object in array by property value
javascript object keys, values
object to array javascript
object.values javascript
javascript key value array
object.keys foreach

how can I get value in an object array by a key, which is also in this object array.

the object array looks like this:

const objectArray = [
    {key: "1", value: "12321"},
    {key: "2", value: "asdfas"}
]

I have now the value of key, e.g. key = 1, but I want to get 12321 as result.

any solution?

You can use .find() to achieve it.

Try this:

Working Demo

this.objectArray.find(x => x.key == "1").value

To handle exception, if the item doesn't exists in the array, do this:

let item = this.objectArray.find(x => x.key == "1")
this.value = item ? item.value : null

Get JavaScript object from array of objects by value of property , This method will return an array instead You simpley use for loop as well, create a function like this: function filteredArray(arr, key, value) { const newArray� Array; Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

You can do this using filter() and use the value of the key you already have.

const objectArray = [
    {key: "1", value: "12321"},
    {key: "2", value: "asdfas"}
]

const el = objectArray.filter(item => item.key == 1)[0];

el
  ? console.log(el.value) // gives 12321
  : console.log('none listed')

How to convert an Object {} to an Array [] of key-value pairs in , Method 1: In this method, we will use Object. keys() and map() to achieve this. Approach: By using Object. keys(), we are extracting keys from the Object then this key passed to map() function which maps the key and corresponding value as an array, as described in the below example. Approach: By using Object.keys (), we are extracting keys from the Object then this key passed to map () function which maps the key and corresponding value as an array, as described in the below example.

objectArray.find(e => e.key == "1")

ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Object.values(), The Object.values() method returns an array of a given object's own the values are returned in the keys' numerical order const arrayLikeObj2 = { 100: my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; }� I have an array which holds multiple values. I also have an object which contains data. Now I want to create an object which only holds the the data which has the same key as the value of the array. const array = ["foo", "bar",

objectArray.forEach(function(item) {
    Object.keys(item).forEach(function(key) {
        console.log("key:" + key + "value:" + item[key]);
    });
});

Get key value array object - JavaScript, Hi all I have a plunker here - https://plnkr.co/edit/gZZ6uh9FZZsTILC1dZVV?p= preview I have simple objects in an array. I just trying to get� On this array of keys, the find () method is used to test if any of these keys match the value provided. The find () method is used to return the value of the first element that satisfies the testing function. If the value matches, then this condition is satisfied and the respective key is returned. This is the key to the value of the object.

jQuery.map(), The first argument to the function is the value; the second argument is the key of the object property. The function can return any value to add to the array. Search should return the object that match the search should return undefined becuase non of the objects in the array have that value should return undefined becuase our array of objects dont have ids 3 passing (12ms) Old answer - removed due to bad practices

Accessing an Object's Keys, Values, and Entries in JavaScript, This will make the salary hidden from the Object.keys. Now we Object.values( obj) → returns an array of a given object's own property values. The Object.values () method returns an array of a given object's own enumerable property values, in the same order as that provided by a forin loop. (The only difference is that a forin loop enumerates properties in the prototype chain as well.) JavaScript Demo: Object.values ()

Object Keys, Values, and Entries: JavaScript ES6 Feature Series (Pt , The method returns an array of a given object's own enumerable property names, in the same order as we get with a normal for loop. Example of� 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.