Compare data between two Arrays containing custom Swift Objects - arrays

I have some data I have received through an API that return JSON to me. I know I can fetch it and store relevant info from the API into my iOS app. But only while the app is running. I.E. I have not implemented YET how to store the info fetched from the API into UserDefalults. Working on this feature I ran into a problem.
I have two Arrays that keeps track of my data. The first Array is the Array I want to store in UserDefaults when I have fetch my data. This one is called "lenders" and keeps LenderData
The second Array is my temporary Array. It contains the same type of objects, and this is the one I want to populate with data from the API and then compare to my existing Array "lenders".
I want to check if the "lenders" Array contains any object that has the same id as the object I'm looking at in the "lendersTemp" array. If the lenders Array does not contain any LederData object with the id of the tempLender we are currently looking at, we add the tempLender into the lenders Array. How would I go about doing this?
My current (non-working) solution is as follows:
var lenders = [LenderData]()
var lendersTemp = [LenderData]()
...
// Get JSON DATA
...
for tempLender in self.lendersTemp {
if !self.lenders.contains(where: {$0.id == tempLender.id}) {
self.lenders.append(tempLender)
}
}
EDIT:
My view did load method:
var lenders = [LenderData]()
var lendersTemp = [LenderData]()
override func viewDidLoad() {
super.viewDidLoad()
downloadJSON {
self.myTableView.reloadData()
}
self.myTableView.rowHeight = 90
myTableView.delegate = self
myTableView.dataSource = self
}

I figured it out with some help! So this is my answer to my own question!
My problem was that getting data from my API is done with an asyc method. And I tried to do comparison after the reload method was called on my TableView. So I did not populate the array the tableview is getting data from, before after the reloadData() had been called and therefore it seemed like my tableview and comparison algorithm, did not work, when in fact it did!

Related

Best approach in moving data from one array to a new array in angular ts

.subscribe((dataChart) => {
// console.log(dataChart)
var forxaxis = []
var cd = [dataChart]
// console.log(cd)
cd.forEach(element => {
forxaxis.push(element.strRequestDate)
console.log(forxaxis)
});
},
Im trying to move my data in the first array into a new array so that I can use it with chart.js. but it didnt work.
dataChart contain 2 column of data. i insert dataChart into an array called cd. then i tried to push one of the column from dataChart which is called strRequestDate into a new array called forxaxis but it just didnt work as per expected. the result is as shown in the image attached.
this is how the data look like. it was called by using sharepoint API
error and the data
You can use array.map property here, so you don't need to push data manually from one array to another
I have taken sample data in dataChart array for demonstration purpose only.
let dataChart = [{strRequestId: 1, strRequestDate: 'ABC'}, {strRequestId: 1, strRequestDate: 'PQR'}];
let forxaxis = dataChart.map(x => x.strRequestDate);
console.log(forxaxis);
Demo
Output:

Perform a task with all of the values in an array (Swift, Firebase)

My situation is that I am creating a social media app, and I have an array of all of the people that the user is following. It looks like this:
let followingArray:[String] = ["user1Uid", "user2Uid", "user3Uid", "user4Uid"]
So, I want to perform a function using every userUid in the array to fetch their posts and display it onto a collectionView.
To give more context the function looks like this (the "userUid" is the element that I need to repeat with all of the userUid's using the array):
databaseRef.child("posts").child(userUid).observeSingleEvent(of: .value) { (snapshot) in
// all of the code to get the posts information into another array to display on the collection view.
}
Thanks a lot!
To do something with every element in an array you can always use forEach(_:). Try:
let followingArray:[String] = ["user1Uid", "user2Uid", "user3Uid", "user4Uid"]
for userID in followingArray {
databaseRef.child("posts").child(userID).observeSingleEvent(of: .value) { (snapshot) in
// all of the code to get the posts information into another array to display on the collection view.
}
}

Why is the array behaving like this

I am working on Angular 6 and i want to post an array of JSON objects using a service class.
The following is my function
getrerun(data,file){
this.post=[];
for(var i=0;i<data.length;i++)
{
this.rerun.storeno=data[i].storeNo;
this.rerun.filetype=data[i].loadtype;
this.rerun.outboundsystem[0]=file;
this.rerun.createdate=data[i].createdDate;
this.post[i]=this.rerun;
console.log("------>"+JSON.stringify(this.rerun)+"------->"+JSON.stringify(this.post));
}
this.newurl=this.devurl+"rerun";
return this.http.post<any>(this.newurl,this.post);
}
newurl is the url of the rest api that i want to hit and this.post is the array that i am sending
The value of data is following:
[{"lastDate":"2019-02-20 12:36:27","storeNo":"G015","country":"SG","serviceStatus":"FAIL","createdDate":"2019-01-04 11:53:56","loadtype":"F"},{"lastDate":"2019-02-20 10:54:00","storeNo":"G121","country":"SG","serviceStatus":"FAIL","createdDate":"2019-01-23 16:29:33","loadtype":"F"}]
and file is 'TP';
However the post array that I am getting is this:
[{"outboundsystem":["TP"],"storeno":"G121","filetype":"F","createdate":"2019-01-23 16:29:33"},{"outboundsystem":["TP"],"storeno":"G121","filetype":"F","createdate":"2019-01-23 16:29:33"}]
this basically means that both the entries in the array are the same i.e this.post[0].storeno is same as this.post[1].storeno. However, they should have two different values.
What do I do about this?

Storing an array in a Firebase Database

I am trying to store an array in the Firebase Database, but I'm not sure how to do it properly.
I am wanting to store an array such as:
var exampleArray = ["item1", "item2", "item3"]
The reason why is I populate a UIPicker with values from an array, and rather than having to push an update to the app each time to add a new value, it would be better if I could just update the database and instantly add the new value to each app.
Is there any way that I could store the values in the database and pull the values from the database and store it into an array as shown above?
Thank you.
The Firebase setValue method will accept an array just the same as a String or Integer. So you can read and write the values in the same way -
var ref: DatabaseReference!
ref = Database.database().reference()
var exampleArray = ["item1", "item2", "item3"]
// Write the array
ref.child("myArray").setValue(exampleArray)
// Read the array
ref.child("myArray").observe(.value) { snapshot in
for child in snapshot.children {
// Add the values to your picker array here
}
}
A simple solution would be read JSON from a Firebase Database Reference containing the array, and deserealize it in swift to a native swift array. See point 2 at https://firebase.google.com/docs/database/#implementation_path

Append new JSON objects to an array in Polymer

I am using a REST API to fetch an array of objects in a Polymer 2.0.2 project. The response is something like this:
[
{"name":"John","city":"Mumbai"},
{"name":"Ron","city":"New York"},
{"name":"Harry","city":"Lisbon"}
]
When the response is received, I set my property named content as follows:
_contentAjaxResponseHandler(event) {
this.set('content', event.detail.response);
}
This works as long as the REST API is called once.
Now, I want to fetch the next batch when user scrolls to the bottom of the page and add it to the existing data.
So, my question is, what is the best way to append new result to the existing content array? Or in other words, What is the best way to merge 2 arrays in polymer?
Till now, the only way I can think of is to loop over the new result and call push method.Something like this:
_contentAjaxResponseHandler(event) {
let newResponse = event.detail.response;
newResponse.forEach(function(newObj){
this.push('content',newObj);
});
}
The following code worked for me:
_contentAjaxResponseHandler(event) {
let newResponse = event.detail.response;
this.set('content',this.content.concat(newResponse));
}

Resources