Search and get information about each value in ReactJs - reactjs

I have an app where i search data depending by name from the input. So, for example: if i select 'John' i get the object that contains info about John.
I want to improve the application, because now if i write 'Mike' or i select 'Mike' i get the object that contains information about Mike (in the console), but if i select the second name, EX: if i will have in the input "Mike John", i get an empty array.
I want to solve this issue plus to save all objects that contains info about each names, when i will click on the button using saveBtn function. How to solve the my issue?
link to my app: https://codesandbox.io/s/divine-frost-skle2

Here is a possible solution to your issue:
https://codesandbox.io/s/vigilant-austin-ih1o8
The main issue was that on your search function you were filtering based on a concatenated string (a result of selectedItems.join(",") and looking for it in the elements array which always returned false and resulted in an empty array.
for example
you were searching for:
"mike,john" inside:
//after JSON.stringify() and toLowerString():
{
"key": "1",
"name": "mike",
"age": 32,
"address": "10 downing street"
},
Some other things I changed in your solution was removing the unnecessary slice, and using the Array.prototype.some() method in order to find the element which matches at least one of your selectedItems
As for saving, I've alerted your search output - but you can do anything you like with it. For example, you can reset the selected items using setState([]);

Related

How can I automatically append a unique field to element of a referenced array every time a new element is added to the source array?

Here's a simple example of what I'm after:
/* source array in clues collection: */
clues = [
{ title: "Islero", answer: 5 },
{ title: "Miura", answer: 17 },
];
/* user collection: */
{
name: "Bob",
userClues: [
{/ref to clues[0]/, solved: true},
{/ref to clues[1]/, solved: false}
]}
{
name: "Wallace",
userClues: [
{/ref to clues[0]/, solved: true},
{/ref to clues[2]/, solved: true}
]}
The primary "clues" array is to be updated and modified as needed, adding or removing clues and changing their values. The "userClues" array in each user's document is to exactly mirror the source clues array by reference, but I'd like to add an additional "solved" field to each element that is unique to each element in each "userClues" array. The "solved" field isn't linked or referenced to anything, each one stands alone. In this example, adding a third element to the "clues" array would add a third element to each "userClues" array along with it's own solved field for each user. Removing this third clue would also remove the entire third element from each "userClues" array.
I'm thinking a schema could be used to append the "solved" field automatically to each "userClues" element, but I'm not sure how you could spread the primary "clues" array into the "userClues" array by way of reference. Every example of referencing I've seen uses the '_id' to link the elements, so I don't see how a schema could apply to a pointer. Any advice or alternative solutions to this problem are greatly appreciated.

Describe LAST item of array in json schema?

I use JSON Schema to describe my data structure
My data contains specific arrays of the following structure:
{
"string1",
"string2",
...
"stringN",
{
"object": "as",
"last": "item"
}
}
I'm wondering how to describe this in json schema (especially the thing "last item is an object")
If I knew the number of string items, "prefixItems" would do the thing (but there could be any number of them).
If the object was the first (not last) item, "prefixItems" together with "items" would work.
If is use "contains", it only checks the object is somewhere in my array, not checking it is the last item.
Seems that I need something like "reversePrefixItems", if such option existed - but it doesn't exists.
So, what is a proper way to describe the last item of an array? (and optionally all the preceding ones - knowing their type but not their total count)
there has been discussion of a proposed keyword postfixItems but it has not yet made it into the spec. I don't think there is a way to do this currently.

What is the best way to store and retrieve a friend's list on Firebase (Swift)

Suppose we have this data
users
uid_0
name: "Hillary"
friends
uid_1: true
uid_2: true
uid_1
name: "Kato"
uid_2
name: "Bill"
Using Swift, how can I retrieve the friends list as an array?
I've thought of using a 'for' loop that runs through the users dictionary and matches the friends, but that would repeat the loop many times. (Not efficient?)
I've also thought of just making a loop that go to the path of the friend's uid and gets it's value from the server.
ref.child("users/uid_1")).observeSingleEventOfType.... etc.
I expect 2 returned objects one containing "name": "Kato" [String: String] and the other containing "name": "Bill"
(sounds like a much less client-intensive method, however that uses significantly more server calls)
Although I thank you for giving me your valuable time, I request that you include real code in your answers if possible.

AngularJS: How to load selected field by ng-repeat

suppose there are so many fields in js object and i want to load only few not all. i was searching on this for getting answer. i got one but i am not sure this is right one.
see the fiddle http://jsfiddle.net/vojtajina/js64b/14/
see this code ng:repeat="(i,th) in head" this is the way where we need to specify something in bracket. i have confusion here. so anyone can help me to understand how to load few selected data with ng-repeat. thanks
If i understand correct you want to know what the (i,th) values print:
First i suggest that you read on the ng-repeat documentation
i is the key of your object and th is the value:
scope.head = {
a: "Name",
b: "Surname",
c: "City"
};
So for this object example that your provided i equals to a,b,c and th equals to "Name", "Surname","City"

Filter by name of element in object

I want to filter an object like so:
<td ng-repeat="(key,value) in contact | contactEditableFields">
<editable-string-field in-edit="contact.editing" original-string="contact[key]" editable-string="contact['editing'+key]">
</td>
With my custom filter looking like:
.filter('contactEditableFields', function (){ |~
return function(contact){ |~
var contactEditFields = {}; |~
angular.forEach(contact, function (v,i){ |~
if (i.search('editing') <0 && i!='pk' && i!='$$hashKey'){ |~
contactEditFields[i]=v;
}
});
return contactEditFields;
};
})
And my contactobject looking something along these lines (attributes in no particular order), and yes both of these types of contacts will happen:
c = { firstName: "John", lastName: "Doe", pk: 1337, editing=false }
or:
c = { firstName: "John", editingfirstName: "Jane", lastName: "Doe", editinglastName:"Doe", pk: 1337, editing=true }
Now this works as I have intended it to - with my intentions being to make it very easy to add new attributes to my "contact" object. However I now have a desire to order the fields being outputted as well in the custom filter (ie do contact['lastName'] before contact['firstName'] before contact['email']), and I understand it's bad practice to rely on my object being ordered when I ng-repeat it.
The ways I've come up to fix it that which I don't like/haven't a clue as to how to do:
-Change contact to an array (probably the worst option, my database is nicely giving me JSON contacts and I don't want to have to mess with that again!)
-Somehow do editing in a better way than just copying the main strings into new attributes and deleting these extra attributes when I'm done
-Hardcode each parameter (am I trying too hard to make this elegant and nice, when it really can't be?)
-Do some filtering magic??
I'm quite new into AngularJS so if any of you experts out there have any experience with this kind of stuff and could point me into which direction I should pursue as well as some help with that direction it would be greatly appreciated!
Consider a solution where you:
Convert your object properties into an array using a filter just before passing it to ng-repeat (ie. don't change original data structure). This is necessary in order to...
Create an additional filter which accepts an array of key names as an argument and returns a filtered and ordered array to ng-repeat
The array of key names could be dynamic and change during run time by the user.
I made a Plunker containing a sample app following those guidelines.
The demo doesn't include your initial object map filtering since the desired key names are passed in inside of an array. But if you still need to use such a filter, say perhaps for defaults before the user reorders, you could add it back in.

Resources