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. Otherwiseundefined
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).
How do I search for an object by its ObjectId in the mongo console?
Not strange at all, people do this all the time. Make sure the collection name is correct (case matters) and that the ObjectId is exact.
Documentation is here
> db.test.insert({x: 1})
> db.test.find() // no criteria
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
> db.test.find({"_id" : ObjectId("4ecc05e55dd98a436ddcc47c")}) // explicit
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
> db.test.find(ObjectId("4ecc05e55dd98a436ddcc47c")) // shortcut
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
Get java object by id
Use HashMap
to keep objects. Then use the get
method to get the object with the given id.
public class Forest {
public String forestName;
HashMap<Integer, Tree> trees = new HashMap<>();
public Forest(String forestName){
this.forestName=forestName;
//initialize trees
trees.put(4, new Tree(4, "redleaef"));
}
public void updateTreeName(int id, String Name){
trees.get(id).treeName = Name;
}
public static void main(String[] args) {
Forest greenforest = new Forest("name");
// create some tree's in the greenforest.
greenforest.updateTreeName(4, "red leafe");
}
}
How to get data by id from an object
You can use Array#find function and pass a condition into it like
arr.find(item => item.id === 1)
Example
const users = [
{id: 1, name: 'A'},
{id: 2, name: 'B'},
];
const user = users.find(item => item.id === 1);
console.log(user);
Related Topics
I Have a Problem with Sending Mail:Typeerror: _Init_() Got an Unexpected Keyword Argument 'Context'
Unicode Box Drawing Characters Not Printed in Ruby
Request Uac Elevation from Within a Python Script
How to Run Script with Elevated Privilege on Windows
Python C Program Subprocess Hangs at "For Line in Iter"
Why Does Id({}) == Id({}) and Id([]) == Id([]) in Cpython
Error: Pandas Hashtable Keyerror
How to Filter Foreignkey Choices in a Django Modelform
Typeerror: 'Module' Object Is Not Callable
Simple Prime Number Generator in Python
Checking Whole String with a Regex
Scope of Python Variable in for Loop
Can't Set Attributes on Instance of "Object" Class