I'm looking for something like: find(pred, iter)
in python
obj = {
"foo_list": [
{"name": "aaaa", "id": 111},
{"name": "bbbb", "id": 222},
{"name": "cccc", "id": 333}
]
}
How to:
find(lambda x: x.get("name") == "bbbb", obj.get("foo_list", []))
Python has a method to search for an element in an array, known as index(). If you would run x. index('p') you would get zero as output (first index).
basically used in array for slicing , understand bracket accept variable that mean value or key to display, and " : " is used to limit or slice the entire array into packets .
The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object. If the method is constructed, then it will available for every array. When constructing a property, All arrays will be given the property, and its value, as default.
Array.prototype.at()The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
def find(pred, iterable):
for element in iterable:
if pred(element):
return element
return None
# usage:
find(lambda x: x.get("name") == "bbbb", obj.get("foo_list", []))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With