Hey I created my first app in react native and i run into some trouble, i simple would like to know how i can check an object for an specific value from a local storage. This is what i have so far, i know its not that much.
handleClick = (item)=>{
store.get('myobject').then((res) =>
console.log(res)
)
this is my res object:
[
{ id: '456', p: 'hey' },
{ id: '464', p: 'ho' },
{ id: '467', p: 'lets' },
{ id: '263', p: 'go' }
]
and as example i would like to know, if id 467 is in the object.
You could make use of the some
method of Array:
var res = [ { id: '456', p: 'hey' }, { id: '464', p: 'ho' }, { id: '467', p: 'lets' }, { id: '263', p: 'go' } ]; var found = res.some(item => item.id === '467'); console.log(found);
Essentially, as it is stated here
The some() method tests whether at-least one element in the array passes the test implemented by the provided function.
You could do a forEach loop and check for your desired value:
var res = [ { id: '456', p: 'hey' }, { id: '464', p: 'ho' }, { id: '467', p: 'lets' }, { id: '263', p: 'go' } ] function containsCheck(array) { var found = false; array.forEach(function(element) { if (element.id === '467') { found = true; } }); return found; }; console.log(containsCheck(res));
作为替代方案,您可以检查Lodash 。使用集合或对象时,此lib是项目中不错的东西。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.