Ruby Nested Array to Javascript - arrays

Guys, it's my first question, :). I'm trying to do as the title says. Here is my code:
in Ruby :
<% _points = Array.new %>
<% #users.each do |user| %>
<% _points.push([user.latitude, user.longitude]) %>
<% end %>
I wanna use _points in a javascript fonction so i do it:
<script>
var points = new Array();
points = <%=raw _points%>;
for (x in points) {
setInterval(function () {
var b = new R.BezierAnim([points[x], citylatlon], {}, function () {
.......................
}, 4000);
}
</script>
finally, the funtction just can worked for le last array in ruby, help! Thx Guys.

i found the way!
var points = <%=_points.to_json%> ;
$(document).ready(function () {
points.forEach(function (x, index, arr) {
animation_voyage(x);
}
)
}
);

Related

how do I retrieve objects from an array to create a whole new arry

I have an array that looks like this.
0:{9 items
"id":"380721"
"group":""
"po_type":NULL
"team1short":"TOR"
"team1long":"Toronto"
"team2short":"OTT"
"team2long":"Ottawa"
"date":"2019-10-03 01:00:00"
"score":{...}3 items
}
1:{9 items
"id":"380731"
"group":""
"po_type":NULL
"team1short":"STL"
"team1long":"St. Louis"
"team2short":"WSH"
"team2long":"Washington"
"date":"2019-10-03 02:00:00"
"score":{...}3 items
}
2:{9 items
"id":"380741"
"group":""
"po_type":NULL
"team1short":"EDM"
"team1long":"Edmonton"
"team2short":"VAN"
"team2long":"Vancouver"
"date":"2019-10-03 04:00:00"
"score":{...}3 items
}
3:{9 items
"id":"380751"
"group":""
"po_type":NULL
"team1short":"VGK"
"team1long":"Vegas"
"team2short":"SJS"
"team2long":"San Jose"
"date":"2019-10-03 04:30:00"
"score":{...}3 items
}
4:{9 items
"id":"380761"
"group":""
"po_type":NULL
"team1short":"TBL"
"team1long":"Tampa Bay"
"team2short":"FLA"
"team2long":"Florida"
"date":"2019-10-04 01:00:00"
"score":{...}3 items
}
Newbie here. This array goes on for duration of a hockey season. I can not for the life of me figure out how to create a new array with games that only contain team1long = Toronto. You can find this in this at games[0]
Any help would stop me from banging my head against the wall relentlessly!
this is the code I was using to loop through all the games. javascript
<% for (i = 0; i <games1.games.length; i++) { %>
<tr>
<th scope="row"><%= games1.games[i].date %></th>
<th><%= games1.games[i].team1long %></th>
<td><%= games1.games[i].score.goals1 %></td>
<td><%= games1.games[i].team2long %></td>
<td><%= games1.games[i].score.goals2 %></td>
</tr>
<% } %>
That was working just fine.
I was trying to run with something like this to create a new arry.
game[] gamesFromToronto;
For(i=0 ; i < games.Length ; i++)
{
if(games[i].team1Long == “Toronto”)
{
gamesFromToronto[gamesFromToronto.length] = games[i];
}
}
After reviewing your code, the logic is correct and should work. But... you made so many mistakes in your code (syntax error and spelling code error) like you start a lot of things with an Uppercase instead of lowercase like :
For --> should be --> for
team1Long --> should be --> team1long ( l and not L )
“Toronto” --> should be --> "Toronto" ( " and not “ )
gamesFromToronto[gamesFromToronto.length] = games[i]; --> should be --> gamesFromToronto.push(games[i]);
so here is a working code that works correctly :
//solution
var gamesFromToronto = [];
for (i=0 ; i < games.length ; i++ ) {
if(games[i].team1long == "Toronto"){
gamesFromToronto.push(games[i]);
}
}
for tests :
//data sample
games = [
{
"id":"380721",
"group":"",
"po_type":null,
"team1short":"TOR",
"team1long":"Toronto",
"team2short":"OTT",
"team2long":"Ottawa",
"date":"2019-10-03 01:00:00"
},
{
"id":"380731",
"group":"",
"po_type":null,
"team1short":"STL",
"team1long":"St. Louis",
"team2short":"WSH",
"team2long":"Washington",
"date":"2019-10-03 02:00:00"
},
{
"id":"380741",
"group":"",
"po_type":null,
"team1short":"EDM",
"team1long":"Toronto",
"team2short":"VAN",
"team2long":"Vancouver",
"date":"2019-10-03 04:00:00"
},
{
"id":"380731",
"group":"",
"po_type":null,
"team1short":"STL",
"team1long":"St. Louis",
"team2short":"WSH",
"team2long":"Washington",
"date":"2019-10-03 02:00:00"
},
{
"id":"380731",
"group":"",
"po_type":null,
"team1short":"STL",
"team1long":"St. Louis",
"team2short":"WSH",
"team2long":"Washington",
"date":"2019-10-03 02:00:00"
}] ;
//solution
var gamesFromToronto = [];
for (i=0 ; i < games.length ; i++ ) {
if(games[i].team1long == "Toronto"){
gamesFromToronto.push(games[i]);
}
}
//visualisation
document.getElementById("demo").innerHTML = JSON.stringify(gamesFromToronto);
<p id="demo"></p>

$state is not defined. Error on entity page generated by jhipster version 4

When I try to change page using pagination bar in an entity generated by Jhipster, get the following javascript error: $state is not defined
I am evaluating the JHipster version 4.0.5 framework with angularJS version 1 to clientFramework. All entities were generated through the command jhipster:entity entity-name using pagination option.
The problem is on the entitycontroller.js generated 'state' variable is missing on the controller injection. e.g.
EntityController.$inject = ['Entity', 'ParseLinks', 'AlertService', 'paginationConstants','$state']
Can you solve this issue for the next release?
You have to modify _entity-management.controller.js
This file is located on the directory:
../node_modules/generator-jhipster/generators/entity/templates/client/angularjs/src/main/webapp/app/entities/_entity-management.controller.js
You need to add $state variable on the $inject and function code. Here you have, the changed code for jhipster 4.0.5 release:
(function() {
'use strict';
angular
.module('<%=angularAppName%>')
.controller('<%= entityAngularName %>Controller', <%= entityAngularName %>Controller);
<%= entityAngularName %>Controller.$inject = [<% if (fieldsContainBlob) { %>'DataUtils', <% } %>'<%= entityClass %>'<% if (searchEngine == 'elasticsearch') { %>, '<%= entityClass %>Search'<% } %><% if (pagination != 'no') { %>, 'ParseLinks', 'AlertService', 'paginationConstants', '$state'<% } %> <%_ if (pagination == 'pager' || pagination == 'pagination'){ %>, 'pagingParams'<% } %>];
function <%= entityAngularName %>Controller(<% if (fieldsContainBlob) { %>DataUtils, <% } %><%= entityClass %><% if (searchEngine == 'elasticsearch') { %>, <%= entityClass %>Search<% } %><% if (pagination != 'no') { %>, ParseLinks, AlertService, paginationConstants, $state<% } %> <%_ if (pagination == 'pager' || pagination == 'pagination'){ %>, pagingParams<% } %>) {
var vm = this;
<%_ if (pagination == 'pagination' || pagination == 'pager') { _%>
<%- include('pagination-template'); -%>
<%_ } else if (pagination == 'infinite-scroll') { _%>
<%- include('infinite-scroll-template'); -%>
<%_ } else { _%>
<%- include('no-pagination-template'); -%>
<%_ } _%>
}
})();
I hope it is useful.

Compare two arrays and concat without duplicates

I have two arrays. I can push and splice by clicking on a word in searchWords, which adds or removes a word to the currentWordlist.
What I want to have is a button that transfers all the searchWords to the currentWordlist, without overwriting the words that are actually on the currentWordlist.
I came up with this code:
$scope.addAll = function () {
var searchWords = [];
var currentWords = [];
// safes all searchwords to the array
for (var i = 0; i < $scope.searchWords.length; i++) {
searchWords.push($scope.searchWords[i]);
}
// safes all currentwords to the array
for (var j = 0; j < $scope.currentWordlist.length; j++) {
currentWords.push($scope.currentWordlist[j]);
}
console.log("searchWords " + searchWords.length);
console.log("currentWords " + currentWords.length);
angular.forEach(searchWords, function(value1, key1) {
angular.forEach(currentWords, function(value2, key2) {
if (value1._id !== value2._id) {
$scope.currentWordlist.push(value1);
}
});
});
};
I go through both of the arrays and safe them so that I can use the arrays inside my two angular.forEach to check if there are duplicates. If I don't push to the currentWordlist. But it's not working. I get an [ngRepeat:dupes] error, but I cannot use track by $index because otherwise removing from the list removes the wrong word. I think I am doing something critically wrong here, but I couldn't find out what so far (hours of trial and error :0)
I would suggest to use angular unique filter with ng-repeat directive. The code could be as follows:
$scope.addAll = function () {
// use angular.copy to create a new instance of searchWords
$scope.combinedWords = angular.copy($scope.searchWords).concat($scope.currentWordlist);
};
And then in your view:
<div ng-repeat="word in combinedWords | unique:'_id'">
{{word}}
</div>
Usage:
colection | uniq: 'property'
It also possible to filter by nested properties:
colection | uniq: 'property.nested_property'
You can simply do like this
angular.forEach($scope.searchWords, function(value1, key1) {
var temp=true;
angular.forEach($scope.currentWordlist, function(value2, key2) {
if (value1.id === value2.id)
temp=false;
});
if(temp)
$scope.currentWordlist.push(value1);
});
var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
$scope.searchWords=[{id:1,name:'A'},{id:2,name:'B'},{id:1,name:'A'},{id:4,name:'D'}];
$scope.currentWordlist=[];
$scope.addAll = function() {
angular.forEach($scope.searchWords, function(value1, key1) {
var temp=true;
angular.forEach($scope.currentWordlist, function(value2, key2) {
if (value1.id === value2.id)
temp=false;
});
if(temp)
$scope.currentWordlist.push(value1);
});
console.log($scope.currentWordlist);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<button ng-click="addAll(newWord)">Add</button>
<div>{{currentWordlist}}</div>
</div>

Unable to compile class for JSP and cannot convert from String to boolean when I use array to make a navbar

I have an idea to create a navigation bar with Arrays and looping it with While.
first, I make a variable on my jsp file like this:
<%
int i, j;
i = 0;
j = 0;
String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";
String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>
The NavMenu is a text for Navbar menu, and NavLink is a link for Navbar menu.
and then, I make my navbar like this:
<%
while (NavMenu[i][0]) { //ERROR ON HERE: cannot convert from String to boolean
j = 1;
//IF MENU HAVE A DROPDOWN
if(NavMenu[i][1]){ //ERROR ON HERE: cannot convert from String to boolean
if(NavMenu[i][0] == ActivePage) { %>
<li class="active dropdown">
<% } else { %>
<li class="dropdown">
<% } %>
<%=NavMenu[i][0]%> <b class="caret"></b>
<ul class="dropdown-menu">
<%
while(NavMenu[i][j]) { //ERROR ON HERE: cannot convert from String to boolean %>
<li>
<%= NavMenu[i][j]%>
</li>
<% j++;
} %>
</ul>
</li>
<%
//IF MENU DO NOT HAVE A DROPDOWN
} else {
if(NavMenu[i][0] == ActivePage){ %>
<li class="active"><%= NavMenu[i][0] %></li>
<% } else {%>
<li><%= NavMenu[i][0] %></li>
<%}
}
i++;
j=0;
}
%>
How to solve this Problem? I have no idea what should I do to make this working.
Java is not like javascript, you can not use string as condition, In java the condition must be in boolean. you can use a for loop to iterate over the String array and do your logic
<%
String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";
String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>
<%
for (int i=0; i<NavMenu.length; i++) { //ERROR ON HERE: cannot convert from String to boolean
String navMenuValue = NavMenu[i][0];
//IF MENU HAVE A DROPDOWN
if(NavMenu[i].length > 1){ //ERROR ON HERE: cannot convert from String to boolean
if(NavMenu[i][0].equals("ActivePage")) { %>
<li class="active dropdown">
<% } else { %>
<li class="dropdown">
<% } %>
<%=NavMenu[i][0]%> <b class="caret"></b>
<ul class="dropdown-menu">
<%
for(int j=0;j<NavMenu[i].length;j++) { //ERROR ON HERE: cannot convert from String to boolean %>
<li>
<%= NavMenu[i][j]%>
</li>
<%
} %>
</ul>
</li>
<%
//IF MENU DO NOT HAVE A DROPDOWN
} else {
if(NavMenu[i][0].equals("ActivePage")){ %>
<li class="active"><%= NavMenu[i][0] %></li>
<% } else {%>
<li><%= NavMenu[i][0] %></li>
<%}
}
}
%>

AngularJS: nested array and view update

I'm having problems updating the view after an Array inside an Array is updated in the $scope.
First i check if the Array member already exists:
$scope.myArray = [];
if(typeof $scope.myArray[someIndex] == 'undefined') {
$scope.myArray[someIndex] = {
name: someName,
data: []
};
}
Then push to $scope.myArray[someIndex].data:
$scope.myArray[someIndex].data.push(dataContent);
At this point the view does not update.
Of course if i directly push to $scope.myArray it does. Any suggestions?
Edit: Fiddle here
It was simpler than it looked.
Based on the response here i am setting an associative array which allows set string keys. If you declare your array as =[] you simply cannot set strings as keys.
So i just changed my declaration $scope.myArray=[] to $scope.myArray={} and voilà, it works.
Try:
if(typeof $scope.myArray[someIndex] == 'undefined') {
$scope.$eval(function(){
$scope.myArray[someIndex] = {
name: someName,
data: []
};
$scope.myArray[someIndex].data.push(dataContent);
});
}
This works:
HTML,
<div ng-app>
<div ng-controller="NestedCtrl">
<article ng-repeat="member in myArray">
{{member.name}}
<article ng-repeat="next in member.data">
{{next.nested}}
</article>
</article>
</div>
</div>
Angular JS:
function NestedCtrl($scope) {
$scope.myArray = [];
var callMe = function(){
if(typeof $scope.myArray[0] == 'undefined') {
$scope.myArray[0] = {
name: 'Hello',
data: []
};
}
$scope.myArray[0].data.push({nested : 'yay'});
}
callMe();
}

Resources