Value of keys when no unique value is present - reactjs

I have an infoBox component in React that needs to be used multiple times.
I have an array of objects.
Each value of the object needs to be passed to the infoBox component leading to the creation of an infoBox.
I am achieving this is by iterating over the object array using map(). I am able to get the output, but I was getting logs in console for unique keys.
Since my object does not have any unique value, I tried using Math.random(). Is it the right approach?
What value do I give for keys, when I do not have unique values in my objects?
userDetails = [{name : "Raj", place : "Chennai"}, {name : "Rani", place : "Mumbai"}, {name : "John", place : "Bengaluru"}]
userDetails.map((user){
return (<InfoBox details = user>);
});
Edit :
userDetails is populated dynamically say based on an organization. When the organization changes, the entire array of objects will be replaced by a new one.
Eg:
userDetails = [{name : "Tom", place : "New York"}, {name : "Pip", place : "Colombo"}, {name : "Davis", place : "Dubai"}]

When you don't have the keys of your object, You can use the index of your loop then doesn't show any warning. It's worked.

You can generate the id with indexOf
userDetails.map((user) => <InfoBox details = {user} key = {userDetails.indexOf(user)}/>)

Related

MongoDB/PyMongo find_one_and_update and/or find() method doesn't find specified entry, just appends to the end of the collection

I'm attempting to search in an array in my collection, to find by a key named "id". Both the find() method and find_one_and_update() don't return the entry I'm trying to find. The collection is structured like so:
"_id" : {stuff},
"events":[{
"id":"12345",
"date":"01/01"
}, {
"id":"12346",
"date":"02/02"
}]
Trying find() as:
result = db.get_collection("events").find({"events.id" : "12345"})
for item in result:
print(item)
Prints out the entire collection.
Trying to update a specific entry by its id like so will append it to the end.
db.get_collection("events").find_one_and_update({"events.id" : "12345"},{"$set" : {"date" : "somedate"}})
Looks like this afterwards:
"id":"12346",
"date":"02/02"
}], "date" : "somedate"
So, what am I doing wrong with updating and finding here? Every other person seems to have no trouble with this part.
Figured this out on my own, needed to specify which object + field to update in the collection:
db.get_collection("events").find_one_and_update({"events.id" : "12345"},{"$set" : {"events.$.date" : "somedate"}})

Joining a series of arrays into one 'parent' array (AngularJS)

I have a series of arrays, filled with objects - in JSON format. They look something like this:
Group 1
[
{ "name" : "John",
"age" : "31"
},
{ "name" : "Bob",
"age" : "33"
}
]
Group 2
[
{ "name" : "Jim",
"age" : "46"
},
{ "name" : "Harry",
"age" : "23"
}
] // ... and so on ...
In Angular, how can I join the two arrays to form an array of arrays? I'm guessing it's group1.concat(group2), or something like that? I'm not sure where to do it though, would I do this in the controller?
Currently I have a $scope property assigned to each variable, would I make a new $scope property that was a concatenated array of each of these?
And would that be something like:
$scope.allGroups = []
$scope.allGroups = $scope.group1.concat($scope.group2)
// since 'allGroups', 'group1', and 'group2' have all been defined can I do...
allGroups = group1.concat(group2) // ...or does $scope need to be used each time?
My intention is (with the necessary filters) to be able to do an ng-repeat through all groups as they will now all be linked to one $scope variable.
I'm pretty sure that's laiden with errors, but I thought it better to provide some bad code than nothing at all, just so it was more evident what I was trying to do. If there are better approaches (which I'm sure there are), I'm all ears.
Thanks in advance
You're right, array1.concat(array2) is the good method to use.
Now the question is, do you need group1 and group2 to be on your $scope ? Do you need to display them ?
If the answer is no, then you could simply do as follow:
Recover the two arrays and store them in 2 "private" variables
Concat them into a variable set into your $scope
You dont have to set variable into your $scope if you dont display them. It will then look like this:
$scope.allGroups = group1.concat(group2)
Otherwise, no other choice than do like you said:
$scope.allGroups = $scope.group1.concat($scope.group2)
EDIT
If you want an array containing the group1 and group2 arrays, and not only their content, you can simply use the push() method as follow:
$scope.allGroups = [];
$scope.allGroups.push(group1, group2);
If you want to be able to access the concatenated array from your views you have to attach the concatenated array in the $scope object, so you will have to use
$scope.allGroups = $scope.group1.concat($scope.group2)
In the case that you leave var allGroups not attached to the $scope object allGroups will be a local variable to the controller function and will be available only through a closure
You can use concat() to join one array with another.
concat() function returns an array.
Here is the code:
$scope.a = [1,2];
$scope.b = [3,4];
$scope.c = $scope.a.concat($scope.b);

FireBase: How to modify the object in database?

I am using AngularFire. Following is the structure of database:
supermarket = {
name : "Food Market",
products : [
{name : "Chocolates"}
{name : "Fruits"}
]
}
I wanna push a vegetable object in products array. How can i do this in firebase?
Stated in Angular Firebase doc for arrays -
As the array is synchronized with server data and being modified concurrently by the client, it is possible to lose track of the fluid array indices and corrupt the data by manipulating the wrong records. Therefore, the placement of items in the list should never be modified directly by using array methods like push() or splice().
Thus you can not use push() or splice().
Firebase gives its own way of adding items to arrays using $add(item) method.
It internally makes sure that the sync is handled in this concurrent environment.
In your case -
If your model which gets sync with firebase server is
supermarket = {
name : "Food Market",
products : [
{name : "Chocolates"}
{name : "Fruits"}
]
}
You get access to this product array array using a firebase ref.
//create a synchronized array
$scope.products = $firebaseArray(ref);
//adding new item to the array
$scope.products.$add({
{name : "vegetables"}
});

ReactJS : Manipulating State doesn't render child components properly

I have three Components "MyApp", "Candidate" and "Courses" and the data is passed from MyApp to Courses.
The data is as such
var data = [{
name : "azhar",
courses : ["Compilers", "Algorithms", "Data Structures"]
}, {
name : "Jenny",
courses : ["Design", "UX"]
}];
And the ReactComponents are rendered as such below :
<MyApp>
<Candidate>
<Courses>
<Courses>
<Courses>
</Candidate>
</MyApp>
Problem : There is a button to add a new Candidate, When clicked I add new Candidate to the state of MyApp. When clicked I see Child components do not render proper data.
Added JSFiddle : https://jsfiddle.net/69z2wepo/8587/
Really hoping someone gives a good explanation, because this is a simple use case.
Thanks
When you create an array of JSX elements like you do with this.state.data.map() you should always include a key attribute that is unique for that position in the array. Note that it shouldn't be something that can change like the index in the array, or the name of the candidate (since there can be duplicates).
If you add an id property on your candidate objects, and create a new id when you add a candidate, it should work: https://jsfiddle.net/qzy3sr6w/2/
Here's a good explanation: http://blog.arkency.com/2014/10/react-dot-js-and-dynamic-children-why-the-keys-are-important/
The TL;DR of that post is that the reason it works for push but not with unshift is because if you don't specify key, React will use the index of the array as key (not exactly true, but close enough). And since you're adding to the beginning of the array, it will use the index 0 which it previously used for another component, meaning that the new first element will get the second elements courses.
I don't think it is a good idea to use react.state directly, as you are doing here:
this.state.data.unshift({
name : "Alice",
courses : [] // No courses for her.
});
Better use additional variable like this:
var candidates = this.state.data;
candidates.push({
name : "Alice",
courses : [] // No courses for her.
});
this.setState({
data : candidates
});
Here is a JSFiidle: https://jsfiddle.net/69z2wepo/8597/

Mongo query item from subarray, having trouble

I'm relatively new to mongodb, and I came into this company's set up that had the database already set up and running. It looks to me like the structure of this "array" isn't actually a proper array. They are saving info that I need in "offer_info_array" - which then has a nested array with an "offer id" which changes a lot between records, and then nested inside that is the info that I need to select. Here is an example of a record.
{
"_id" : ObjectId("52041af3bbf8057203000004"),
"offer_info_array" : {
"128" : {
"affid" : "68",
"s1" : "YJF"
}
},
"city" : "Cleveland",
"state" : "OH",
"zip" : "44111"
}
So from a whole db of records like this one, I need to find all records that have the "affid" of "68" - I realize this database is not structured correctly, but there's not much I can do about that for records that already exist. The "128" is the offer id that varies from record to record.
If anyone has any insight and can help me out with this, I would greatly appreciate it. Thank you!
You can use $where operator which accepts JavaScript function:
db.items.find({$where: function() {
for(var key in obj.offer_info_array)
if(obj.offer_info_array[key].affid == 68)
return true;
return false; }})
This function looks for properties of offer_info_array object and gets value of property by key. Then we verify if property value has affid property equal to 68. If yes, we return true which means objects matches our query. If there is no properties with affid equal to 68, we return false.
Keep in mind, that $where operator do not use indexes.

Resources