Can't add objects in array Angular - arrays

I have Java service which retrieves table from oracle database and I want to display the result in Angular application, also I have an array of Objects in Angular:
resultSet:Info[]=[];
service:
pastHourInfo() {
const url='/SearchApp-1.0/users/transaction';
return this.http.get(url).pipe(map((data:any)=>data));
}
this is my service subscribtion:
checkPasHourInfo() {
this.searchService.pastHourInfo().subscribe(data => {
console.log("we got: ",data);
this.resultSet.push(data);
console.log(this.resultSet.length);
},
error => {
console.log("Error",error);
},);
Problem is the next. The result is 77 rows .
console.log("we got: ",data) gives correct result. you cans see it here
but console.log(this.resultSet.length); prints "1" when it must be 77.
What is the problem?

From your screenshot it seems that your are pushing the array into your result array, you could spread your data into the array as such:
this.resultSet.push(...data);

You are pushing an array into an array. So your array looks like this
resultSet[].length = 1;
resultSet[0].length = 77;
Instead of doing:
this.resultSet.push(data);
try this:
this.resultSet.concat(data);
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

You can add array into another array as below:
this.resultSet.concat(data)

Related

how to cast json to array in angular

I have file1.ts which has a variable menuGroups
menuGroups= Array();
i populate menuGroups with data so it looks something like this
i store this in localstorage : localStorage.setItem("menuGroups", JSON.stringify(this.menuGroups));
now in file2.ts, i want to assign above localstorage value to an array variable at class level. how to achieve it?
basically , in file2.html, i want to loop through the array menuGroups variable using ngFor.
i tried this in file2.ts but getting error
menuGroups: any[] = [];
populateNavBar() {
this.menuGroups = JSON.parse(localStorage.getItem('menuGroups')) || [];
}
Working ex
If you try to change
menuGroups: any;
Show run example stackblitz here.

Adding an Array to an Array in Angular

So I have a db document that holds some string values in an array, I want to push just the array from every entry into an array in the application for usage later, But I can see the array fine on the fetch, and when I iterate it but my "Global array" is staying empty can someone explain why?
specialDates : Specialdates[] = [];
specialRange: any[] = [];
this.specialDates.forEach(ag => {
//ag,range -> I can see fine
this.specialRange.push(ag.range);
//this.specialrange -> Stays empty
});
Array looks something like the following:
1205,1206,1207,1208
What is wrong with this approach?
Reason for doing this is because the documents have 2 fields minimum: EG ->
ID/Array
And I just need the array
this.specialRange = this.specialDates.map(ag => ag.range)

Reduce the size of array that directly get from API

in my app, I get an array from an API
do {
var data = try JSONDecoder().decode([NameInfo].self, from: data!)
data.sort{$0.updated_at! > $1.updated_at!}
completion(.success(data))
} catch {
print(error)
}
for example in this data contains about 600 items which take a long time to load in the app, I just want to reduce the size of this array to 50 items directly here that this reduced array goes to the app to show.
I tired to use this method
let limitedData = data.prefix(50)
completion(.success(limitedData))
but this error shows up:
Member 'success' in 'Result<[NameInfo], Error>' produces result of
type 'Result', but context expects
'Result<[NameInfo], Error>'
Could anyone help me on that?
Thanks
Your Result expects [NameInfo] as Success type. prefix() method returns ArraySlice<NameInfo>. You need to create array from your limitedData:
let limitedData = data.prefix(50)
completion(.success(Array(limitedData)))

getting data from an object's array with vue.js

I am trying to access the following data in Vue.js
{"id":1,"name":"Westbrook","created_at":"2017-06-10T16:03:07.336Z","updated_at":"2017-06-10T16:03:07.336Z","stats":[{"id":1,"player_id":1,"points":2558,"assists":840,"rebounds":864,"created_at":"2017-06-10T16:03:07.373Z","updated_at":"2017-06-10T16:03:07.373Z"}]}
self.player = response.name works. now i need self.point
methods: {
fetchData: function() {
var self = this;
$.get("api/v1/players/1", function(response) {
console.log(response);
self.player = response.name;
self.point = response.stats.points
});
}
}
I have thus far tried response.stats["points"], response.stats[2], response.stats[ { points } ], response.stats[points]
The stats property in your json holds an array in which the first object has a property named points
So use response.stats[0].points
Keep in mind though that the stats is probably an array for a reason. It might hold more than one objects in the future, so always using the first element [0] might not be a valid approach.
I think it can help you
var json = JSON.parse(data);
json.stats[0].points
response = {"id":1,"name":"Westbrook","created_at":"2017-06-10T16:03:07.336Z","updated_at":"2017-06-10T16:03:07.336Z","stats":[{"id":1,"player_id":1,"points":2558,"assists":840,"rebounds":864,"created_at":"2017-06-10T16:03:07.373Z","updated_at":"2017-06-10T16:03:07.373Z"}]}
If you want to access the name
console.log(response.name) // Westbrook
If you want to access the stats data which contain list, simply target
let stats=response.stats[0] //By getting the first index in the list
Get the points in stats
console.log(stats.points) // 2588

Sequelize doesn't return anything if value for $notIn is an empty array but it returns me if array contains an empty value

I want Sequelize to return to me all the values which IDs are not in an array.
Sequelize doesn't return anything if value for $notIn is an empty array but it returns me if array contains an empty value.
This returns me nothing:
db.foo.findAll({
where: {
id: {
$notIn: []
}
}
});
This returns me every value:
db.foo.findAll({
where: {
id: {
$notIn: ['']
}
}
});
How come it doesn't return all values if the array is empty? If it is empty, then it means all values which values are not in that array should be returned. Since the ID doesn't contain any value, sequelize should return to me all the values, right?
This issue will be fixed in Sequelize version 4. That version has not been fully disclosed yet, though. At least there is no documentation yet.
See the GitHub issue at https://github.com/sequelize/sequelize/issues/4859.
The changelog can be found at https://github.com/sequelize/sequelize/blob/master/changelog.md. It says
[FIXED] $notIn: [] is now converted to NOT IN (NULL) #4859
Maybe you can implement those version 4 changes in your current code yourself.
For those using < 4 version, here is a work around I used,
let idsToSkip = [...]; //generate array of ids, this may result in empty array
idsToSkip.push('-1'); // push a value that can not be the id in table. this will generate `not in ('-1')` query serving the purpose.
db.foo.findAll({
where: {
id: {
$notIn: idsToSkip
}
}
});
Hope it helps !!

Resources