Append array element to String(Swift) - arrays

I am trying to make an array of random numbers for an ID for 25 people so I figured that would be the easiest way would use a array map but is there a way to take each element and attach it to an "email" in the dictionary key.
class ClassRosterModel {
var randomArray = (1...25).map{_ in arc4random()}
var studentsRoster = [Dictionary<String, String>] ()
init () {
studentsRoster.append(["name": "Kacz, Alex", "major" : "SE", "email" : ".join(randomArray[0])#email.edu", "currentTerm" : "Spring", "numberOfCredits" : "" ])
studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : ".join(randomArray[0]#email.edu", "currentTerm" : "Fall", "numberOfCredits" : "" ])
}
}

From what I am understanding, you want to put a value inside a string. You can use \().
Example:
let value = 101
let email = "myname\(value)#email.com"
print(email)
The console result will be:
myname101#email.com
Just use your randomArray instead of value. And save the email inside your dictionary.
studentsRoster.append(["name": "Kacz, Alex", "major" : "SE", "email" : ".join\(randomArray[0])#email.edu", "currentTerm" : "Spring", "numberOfCredits" : "" ])
studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : ".join\(randomArray[0])#email.edu", "currentTerm" : "Fall", "numberOfCredits" : "" ])

Related

Arabic text in swift

i have array of objects like this
"sub-specialty" : [
{
"id" : 1,
"title" : "Adult Physiotherapy",
"ar_title" : "علاج طبيعي بالغين",
"name" : "علاج طبيعي بالغين"
},
{
"id" : 2,
"title" : "Pediatric Physiotherapy",
"ar_title" : "علاج طبيعي اطفال",
"name" : "علاج طبيعي اطفال"
},
{
"id" : 3,
"title" : "Sport Injuries",
"ar_title" : "اصابات ملاعب",
"name" : "اصابات ملاعب"
},
{
"id" : 4,
"title" : "Rehabilitation",
"ar_title" : "تأهيل",
"name" : "تأهيل"
}
]
when i use map to grab name only like this
let sub = sub-specialty.map({($0.name ?? "")}).joined(separator: ", ") ?? ""
i got rubbish characters or text is flipped please see the attached image
enter image description here
You need to get the text as String and set wherever you want to set. It will work fine. I have worked with multi languages and this work for me.

Python get a value in JSON array

In Python 3-x, consider you have an array in JSON syntax:
members = '''[
{
"name" : "Amber",
"age" : 5
},
{
"name" : "Becky",
"age" : 4
}
]'''
How do you get the value for age where the name is Amber? (The answer should be 5).
variable members look like a string so first change string to json object and search what you want.
members = '''[
{
"name" : "Amber",
"age" : 5
},
{
"name" : "Becky",
"age" : 4
}
]'''
import json
obj = json.loads(members) #Changing string to json
for some_variable in obj:
if some_variable['name'] == 'Amber':
print (some_variable['age']) # will print 5

Find element inside array and return only array

I'm new to MongoDB, so i'm having some newbie problems.
I have this document on DB
{
"_id" : ObjectId("5a7ba73dbf27878474ac7682"),
"Enterprise" : "SpaceX",
"Address" : "qwerty",
"users" : [
{
"name" : "Elon",
"number" : "123456",
"email" : "elon#mars.com",
"user" : "elon",
"pass" : "byeroadster"
},
{
"name" : "Larry",
"number" : "3215465",
"email" : "larry#google.com",
"user" : "larry",
"pass" : "googlepassword"
}
]
}
And i'm doing this query
var db = client.db('hauz_manager');
var findUser = db.collection('system_enterprise');
findUser.find({'users.user': data.user},{contatos: 1}).toArray(function(err, result){
console.log(result.users);
});
With that, i'm having like
Enterprise: "SpaceX"
...
users: [{Object}]
And, i'm not able to do some result.users, returns undefined.
tnx.
console.log(result.users)
I think your callback return array.
You should check callback.
Do you need a loop?

es6 Loop through object array and aggregate unique values from key pair (cleanest way)

What's the cleanest and es6 native (if possible) way to loop through a object array to grab each unique value. Example would like this :
[{
"name" : "joe",
},
,{
"name" : "jean",
},
{
"name" : "joe",
},
{
"name" : "joe",
},
{
"name" : "mike",
}]
and in my results I want to see only : joe, jean, mike (only unique values, no dupes)
Since you mentioned ES6, it seems like a Set object would be what you want since it will do the uniqueness part for you and should do so fairly efficiently:
var objs = [{"name" : "joe"},{"name" : "jean"},{"name" : "joe"},{"name" : "joe"},{"name" : "mike"}];
let uniqueNames = Array.from(new Set(objs.map(item => item.name)));
console.log(uniqueNames);
Run this snippet to see the results.
a = [{name:"joe"},{name:"jean"},{name:"joe"},{name:"joe"},{name:"mike"}]
console.log(_.uniq(_.map(a, 'name'))) // Lodash 0.1.0
console.log([...new Set(a.map(o => o.name))]) // ES6
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.4/lodash.min.js"></script>
var objs = [{"name" : "joe"},{"name" : "jean"},{"name" : "joe"},{"name" : "joe"},{"name" : "mike"}];
var uniqueNames = objs.map( obj => obj.name )
.filter( (name, idx, arr) => { return arr.indexOf(name) === idx; } );
The .map extracts an array of the name values, and the .filter returns only the unique elements (first instances only).

update nth document in a nested array document in mongodb

I need to update a document in an array inside another document in Mongo DB
{
"_id" : ObjectId("51cff693d342704b5047e6d8"),
"author" : "test",
"body" : "sdfkj dsfhk asdfjad ",
"comments" : [
{
"author" : "test",
"body" : "sdfkjdj\r\nasdjgkfdfj",
"email" : "test#tes.com"
},
{
"author" : "hola",
"body" : "sdfl\r\nhola \r\nwork here"
}
],
"date" : ISODate("2013-06-30T09:12:51.629Z"),
"permalink" : "jaiho",
"tags" : [
"jaiho"
],
"title" : "JAiHo"
}
Q1) Update email of 0th element of comments array
db.posts.update({"permalink" : "haha"},{$set:{"comments.0.email":1}})
This doesn't throw any exception but doesn't update anything as well
Q2) Add a field on nth element of comments array number_likes
db.posts.update({"permalink" : "haha"},{$inc:{"comments.0.num_likes":1}})
Doesn't work either.
Am I missing something here?
Q1: If you update with permalink 'jaiho' instead of 'haha', it most certainly updates the email;
> db.posts.update({"permalink" : "jaiho"},{$set:{"comments.0.email":1}})
> db.posts.find()
..., "email" : 1 },...
Q2: Same goes for this include;
> db.posts.update({"permalink" : "jaiho"},{$inc:{"comments.0.num_likes":1}})
> db.posts.find()
..., "num_likes" : 1 },...
If you are trying to do it dynamically in Node JS following should work.
i = 0;
selector = {};
operator = {};
selector['comments.' + i + '.email'] = 1; // {'comments.0.num_likes' : 1}
operator['$inc'] = selector; // {'$inc' : {'comments.0.num_likes' : 1} }
db.posts.update({'permalink' : 'xyz'}, operator);

Resources