How to Find Entry by Object Property from an Array of Objects

Find object by id in an array of JavaScript objects

Use the find() method:

myArray.find(x => x.id === '45').foo;

From MDN:

The find() method returns the first value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.


If you want to find its index instead, use findIndex():

myArray.findIndex(x => x.id === '45');

From MDN:

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.


If you want to get an array of matching elements, use the filter() method instead:

myArray.filter(x => x.id === '45');

This will return an array of objects. If you want to get an array of foo properties, you can do this with the map() method:

myArray.filter(x => x.id === '45').map(x => x.foo);

Side note: methods like find() or filter(), and arrow functions are not supported by older browsers (like IE), so if you want to support these browsers, you should transpile your code using Babel (with the polyfill).

Select a property from an array of objects based on a value : Javascript

You can use reduce and check if the checked property is true, then push (As pointed out by assoron) the value to the accumulator - there is no need for 2 loops:

const arr = [  { "value": "abc", "checked": true },  { "value": "xyz", "checked": false },  { "value": "lmn", "checked": true }]
const filtered = arr.reduce((a, o) => (o.checked && a.push(o.value), a), []) console.log(filtered)

Find a value in an array of objects in Javascript

You can loop over the array and test for that property:

function search(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].name === nameKey) {
return myArray[i];
}
}
}

var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];

var resultObject = search("string 1", array);

efficient way to search object in an array by a property

You can iterate that objects:

function findObjectById($id){
$array = array( /* your array of objects */ );

foreach ( $array as $element ) {
if ( $id == $element->id ) {
return $element;
}
}

return false;
}

Edit:

Faster way is to have an array with keys equals to objects' ids (if unique);

Then you can build your function as follow:

function findObjectById($id){
$array = array( /* your array of objects with ids as keys */ );

if ( isset( $array[$id] ) ) {
return $array[$id];
}

return false;
}

Accessing Object Property Values Within an Array - JavaScript

You can acces items in arrays at given position by their index. In javascript indexes of arrays are starting with 0: myArray[0]. To access the property of the returned object just use dot-notation: myArray[0].myProperty.

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];
console.log(object1[1].job);

search for a specific property in array of objects and return boolean

You can use some. So basically it will return a boolean value if any of the object property in array matches the required value

let arr = [{
name: "john",
age: 22
},
{
name: "george",
age: 55
}
];
let obj = {
name: "bill",
age: 55
}

const val = arr.some(item => item.age === obj.age);
console.log(val)

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

Here is a shorter way of achieving it:

let result = objArray.map(a => a.foo);

OR

let result = objArray.map(({ foo }) => foo)

You can also check Array.prototype.map().

How to check if property of an objects of array matches with one of the values in another array of object

First off, you'll have to make sure if the ID types are correct. Users has Number type for IDs but attendingUsers has String type.

Let's say they're both the same type for the code below (I'm going with string).

You can turn the attendingUsers array into an array of strings with:

const attendingUsersIds = attendingUsers.map(attendingUser => attendingUser.id)

Then match the ids with:

const matchedUsers = users.filter(user => attendingUsersIds.includes(user.id))

If they're intended to not be the same type, you can use user.id.toString() to turn the Number into a String or parseInt(attendingUser.id) to turn the String into a Number.



Related Topics



Leave a reply



Submit