I have an issue and this is not something that I have ever used before.
This is a snippet of the code.
//Defined at top of file
var ships_array = [];
//Function later on
function refreshJSON(){
$.getJSON("scripts/getJSON.php", function(json){
if(json){
$.each(shiplist,function(i,item){
$.each(item,function(j,subitem){
var mmsi = subitem['mmsi'];
ships_array[mmsi].lat = subitem['lat'];
ships_array[mmsi].lon = subitem['lon'];
});
});
}
});
}
The error I am getting is
ships_array[mmsi] is undefined [Break
On This Error] ships_array[mmsi].lat =
subitem['lat'];
Is this the right way to use an associative array? I have seen an example similar to this but I am going wrong somewhere! At the end of it I want an array of Google Maps markers!
you just need to initialize the value at the hash key first
var mmsi = subitem['mmsi'];
ships_array[mmsi] = ships_array[mmsi] || {};
ships_array[mmsi].lat = subitem['lat'];
ships_array[mmsi].lon = subitem['lon'];
You can change your code like this:
//Defined at top of file
var ships_array = [];
//Function later on
function refreshJSON(){
$.getJSON("scripts/getJSON.php", function(json){
if(json){
$.each(shiplist,function(i,item){
$.each(item,function(j,subitem){
var mmsi = subitem['mmsi'];
ships_array[mmsi] = {lat: subitem['lat'], lon:subitem['lon']};
});
});
}
});
}
Related
I have a scenario where I am passing an array of objects to a function in nodejs, but the same is failing with undefined error.
Here is what I have tried :
var object = issues.issues //json data
var outarr=[];
for(var key in object){
outarr.push(object[key].key)
}
console.log(outarr) // array is formed like this : ['a','b','c','d','e']
for(var i =0; i<outarr.length;i++){
jira.findIssue(outarr[i]) //here I am trying to pass the array objects into the loop one by one
.then(function(issue) {
var issue_number = issue.key
var ape = issue.fields.customfield_11442[0].value
var description = issue.fields.summary
var ice = issue.fields.customfield_15890[0].value
var vice = issue.fields.customfield_15891.value
var sor = issue.fields.labels
if (sor.indexOf("testcng") > -1) {
var val = 'yes'
} else {
var val = 'yes'
}
var obj = {};
obj['ape_n'] = ape;
obj['description_n'] = description;
obj['ice_n'] = ice;
obj['vice_n'] = vice;
obj['sor_n'] = val;
var out = {}
var key = item;
out[key] = [];
out[key].push(obj);
console.log(out)
} })
.catch(function(err) {
console.error(err);
});
});
What I am trying to achieve : I want to pass the array values as a parameter which is required by jira.findissue(bassically passing the issue number) one by one and which should again fetch the values and give a combine json output.
How can I pass this array values one by one in this function and also run jira.findissue in loop.
Any help will be great !! :-)
I have taken a look at the code in your question.
To be honest the code you wrote is messy and contains some simple syntax errors.
A good tip is to use a linter to avoid those mistakes.
More info about linters here: https://www.codereadability.com/what-are-javascript-linters/
To output all results in one array you have to define the array outside the scope of the loop.
I cleaned the code a bit up and use some es6 features. I don't know the context of the code but this is what I can make off it:
//map every value the key to outarr
let outarr = issues.issues.map( elm => elm.key);
//Output defined outside the scope of the loop
let output = [];
//looping outarr
outarr.forEach( el => {
jira.findIssue(el).then(issue => {
//creating the issue object
let obj = {
ape_n: issue.fields.customfield_11442[0].value,
description_n: issue.fields.summary,
ice_n: issue.fields.customfield_15890[0].value,
vice_n: issue.fields.customfield_15891.value,
sor_n: issue.fields.labels.indexOf("testcng") > -1 ? "yes" : "yes",
};
//pushing to the output
output[issue.key] = obj;
}).catch(err => {
console.log(err);
});
});
//ouputing the output
console.log(output);
Some more info about es6 features: https://webapplog.com/es6/
I wanted to show all the things inside my array, but it only shows the latest loops. Below is my controller function:
/*global angular*/
var app = angular.module('statisticsApp', []).controller('myCtrl',
function ($scope, $http) {
"use strict";
return $http({
method : "POST",
url : "GatewayAPI.php",
}).then(function mySuccess(response) {
$scope.records = response.data;
var mydata,myJSON,myresult,myjava, myobj;
var i;
var Result;
var chartResultTemp = [];
var resultType = [];
for(i=0; i<72;i++)
{
//storing data
mydata = $scope.records.data[i];
//retrieving data
Result = mydata.data.substring(6,9); //throw this in
myobj = mydata.data.substring(3,4);
resultType = mydata.data.substring(3, 4);
if(resultType === "A") { //selects type = a
chartResultTemp = mydata.data.substring(6,9);
} ;
$scope.test2=chartResultTemp; //this one
$scope.test3 = resultType;
console.log(Result);
console.log(resultType);
}
$scope.gotTemp = false;
$scope.gotHumidity = false;
$scope.getSoilMoisture = false;
});
});
This is the code in my php where i list all the items in my array:
<ul>
<li data-ng-repeat="cd in test2">{{cd}}</li>
</ul>
And this is my current result as viewed in browser console:
As you can see from the console log, it only shows the latest loop. I wanted to show all the data inside the array. Is it because I have declared the array wrongly.
UPDATE:
so I have used $scope.test2.push(chartResultTemp), it gave me an error: Cannot read property 'push' of undefined.
Then, i tried
if(resultType === "A") {
chartResultTemp.push([mydata.data.substring(6,9)]);
} ;
It works, but the problem is the items listed has brackets in it.
You need to push to the array
$scope.test2.push(chartResultTemp);
By using (=) you are assigning different values to the same scope in each iteration which ultimately holds the final value assigned to it. To store all the values, you have to create an array and push value from each iteration.
$scope.test2 = [];
for(let i=0; i<72; i++){
.....
.....
$scope.test2.push(chartResultTemp);
.....
.....
I have a firebase set up. here is the structure:
I am having trouble getting the 'newNroomID' value (that is a6QVH, a7LTN etc..).
this value will be use to compare with the other variable value.
I know that in javascript, to access the value of the object it can be done like this:
var card = { king : 'spade', jack: 'diamond', queen: 'heart' }
card.jack = 'diamond'
but it seems different story when it comes with the firebase or surely enough i am missing something. Here is my code.
var pokerRoomsJoin = firebase.database().ref(); // this is how i set it up this block of code is for reading the data only
pokerRoomsJoin.on('value', function(data){
var rID = data.val();
var keys = Object.keys(rID);
var callSet = false;
for (var i = 0 ; i < keys.length; i++) {
var indexOfKeys = keys[i];
var roomMatching = rID[indexOfKeys];
var matchID = roomMatching.newNroomID; // this does not work alwaus give me undefined
console.log('this return :' + matchID + ' WHY!')
console.log(roomMatching)
if(matchID == 'ffe12'){ // 'ffe12' is actually a dynamic value from a paramiter
callSet = true;
}
}
})
and here is the result of the console log:
strangely i am able to access it like this
var matchID = roomMatching.newNroomID // it return a6QVH and a7LTN one at a time inside the loop
only if i set up the ref to :
var pokerRoomsJoin = firebase.database().ref('room-' + roomId);
I've tried searching but seems different from the structure i have . am I having bad data structure? Save me from this misery , thanks in advance!
Let us see the code inside for loop line by line,
1. var indexOfKeys = keys[i];
now indexOfKeys will hold the key room-id
2. var roomMatching = rID[indexOfKeys];
here roomMatching will hold the object
{ 'firebasePushId': { newDealerName: 'b',
...,
}
}
Now
3. var matchID = roomMatching.newNroomID;
This of-course will be undefined because roomMatching has only one
property , firebasePushId.
To access newNroomID , you have to do something like this,
matchID = roomMatching.firebasePushKey.newNroomID .
One way to get firebasePushKeys will be using Object.keys(roomMatching).
I want to get some properties of one of my div via following code
(function ($) {
function StickyNotes() {
this.getProperties = function (note) {
var properties = {};
properties['top'] = note.position().top;
properties['from_center'] = this.calcFromCenter(note.position().left);
properties['width'] = note.find(".resize").width();
properties['height'] = note.find(".resize").height();
return properties;
}
this.saveBoardAndNotes = function (board_id, board_name) {
var noteList = new Array();
$(".optimal-sticky-notes-sticker-note").each(function(){
// Replace plain urls with links and improve html
var note = $(this);
content = note.find(".textarea").html();
noteID = note.attr("id");
properties = JSON.stringify(this.getProperties(note));
});
}
}
var StickyNotes = new StickyNotes();
jQuery(document).ready(function (e) {
$('#sticky-notes-add-board').click(function (e) {
if(confirm('Do you want to save previous board?')) {
var board_id = $('.optimal-sticky-notes-board:last').attr('id');
var board_name = $('.optimal-sticky-notes-board:last').text();
StickyNotes.saveBoardAndNotes(board_id, board_name);
}
})
});})(jQuery);
But I get following error..
TypeError: this.getProperties is not a function
I filtered all data like content and noteID. They are showing. But problem with this.getProperties. How can i solve the problem. Thanks in advance.
Inside each loop this points to current optimal-sticky-notes-sticker-note div. One of the several ways to reference correct scope is to use variable pointing to outer this:
this.saveBoardAndNotes = function (board_id, board_name) {
var noteList = new Array();
var self = this;
$(".optimal-sticky-notes-sticker-note").each(function(){
// Replace plain urls with links and improve html
var note = $(this);
content = note.find(".textarea").html();
noteID = note.attr("id");
properties = JSON.stringify(self.getProperties(note));
});
}
I've got this problem i can't solve.
I want to make the part that says "aboutMe" a variable called page_name (after the parameter), but I can't get it working...
$(document).ready(function(page_name) {
$.getJSON('../json/text.json', function(obj) {
// fill the title
for(i in obj.titles) {
var id1 = obj.titles[i].id;
var title1 = obj.titles[i].title;
$("#"+id1+"_title").append(title1);
}
// fill the rest of the page
for(i in obj.pages.aboutMe.page_element) {
var id = obj.pages.aboutMe.page_element[i].id;
var title = obj.pages.aboutMe.page_element[i].title;
var content = obj.pages.aboutMe.page_element[i].content;
var left_content = obj.pages.aboutMe.page_element[i].content_left;
var right_content = obj.pages.aboutMe.page_element[i].content_right;
$("#"+id+"_title").append(title);
$("#"+id+"_content").append(content);
$("#"+id+"_left_content").append(left_content);
$("#"+id+"_right_content").append(right_content);
}
for(var i in obj.pages.study) {
}
});
});
Does Anyone know how to solve this?
I would be very gratfull!
What is the error? Try using $(document).ready(function(){}); syntax. It may happen that there is a Jquery conflict. You can also resolve that by JQuery(document).ready(function(){});