Access Json outside of javascript each function - javascript

below is my script,
var holidays = {};
var count=0;
$('.row').each(function(index) {
var names = document.getElementById('name['+count+']').value;
var dates = document.getElementById('dateh['+count+']').value;
holidays[names] = dates;
count ++;
console.log(holidays);
});
If i do console.log(holidays); inside the function i can get the json object, but if i do the same outside of $('.row').each(function(index) it's not printing any.
Did anyone know where i'm going wrong ?

declare array like
var holidays = [];
or
var holidays = new Array();

Related

JavaScript traverse Json array and select different attribute to save but get same array

I'm not good at JavaScript and I meet a problem and It bothers me a lot.Hope someone can help me.
Here is the JS code:
<script>
var data = [
{"":"AT1G61780","baseMean":"849.058775137712","log2FoldChange":"-8.90368140154262","lfcSE":"0.466042198257675","stat":"-19.104882422299","pvalue":2.29944969924087e-81,"padj":5.06798713712688e-77},
{"":"AT5G37130","baseMean":"4294.59961310424","log2FoldChange":"3.27327875544505","lfcSE":"0.182729052190661","stat":"17.9132913797949","pvalue":9.28774588475587e-72,"padj":1.0235095965001e-67},
{"":"AT2G22330","baseMean":"1462.21154035855","log2FoldChange":"-2.04203848172526","lfcSE":"0.19028685011923","stat":"-10.7313694059561","pvalue":7.25135115276992e-27,"padj":5.32732598023497e-23},
{"":"AT4G39950","baseMean":"1767.03303498768","log2FoldChange":"-1.71531773333609","lfcSE":"0.18987118053785","stat":"-9.03411317334783","pvalue":1.65337563321386e-19,"padj":9.11009973900836e-16},
{"":"AT4G07850.1","baseMean":"157.215533895214","log2FoldChange":"-3.41991052916375","lfcSE":"0.384125650856733","stat":"-8.90310376705167","pvalue":5.43063674541315e-19,"padj":2.39382467737812e-15},
{"":"AT4G33467","baseMean":"164.107317842216","log2FoldChange":"-3.88623993122809","lfcSE":"0.469140396775025","stat":"-8.28374609806141","pvalue":1.19359945117813e-16,"padj":4.38448865066102e-13}];
var sbt = log2FC = pp = [];
for(var i = 0; i < data.length; i++){
sbt.push(data[i][""]);
log2FC.push(data[i]["log2FoldChange"]);
pp.push(data[i]["padj"]);
};
console.log(sbt);
console.log(log2FC);
console.log(pp);
</script>
I want to use JS to traverse JSON data to respective save each member object three value(object[""],object["log2FoldChange"],object["padj"]) to three different array.But finally I console.log the result to find the three array are totally same like the picture show.I don't know why.Thanks Advance!!!
Because when you declare data like:
var sbt = log2FC = pp = [];
It actually means:
pp = [];
log2FC = pp;
var sbt = log2FC
So, you are basically assigning same empty array as a reference to all the three variables. So, data pushed in any array pp, log2FC or sbt is actually pushed to all three of them, thus you are getting the issue.
You can resolve it by declaring variables like:
var sbt = [], log2FC = [], pp = [];
Declare your arrays like this instead:
var sbt = [];
var log2FC = [];
var pp = [];
Otherwise you're just assigning them all to each other

Cookie name and value into Array

I'm trying to get all of the domain's cookies and store the name and the value into two separate arrays. Let's say I got the cookies with document.cookie and parsed it into an array:
["cookie0=value0","cookie1=value1"]
How could I extract the name and the values and turn it into two separate arrays? Should be like this:
var cookie_names = ["cookie0","cookie1"]
var cookie_values = ["value0","value1"]
I've tried to extract them with .split, but have no clue how to implement them.
Can you try out this function
function myFunction () {
var cookie_names = [];
var cookie_values = [];
var cookies = ["cookie0=value0","cookie1=value1"];
var count = 0;
for(var i in cookies){
cookie_names[count] = cookies[i].split("=")[0];
cookie_values[count] = cookies[i].split("=")[1];
count++;
}
console.log(cookie_names);
console.log(cookie_values);
}
But you have to make sure that cookies array should be in the proper format so that it can be splitted by =. In my opinion, it would be much easier of you can make the cookies array an JS Object.
eg: var cookies = {"cookie0" : "value0", "cookie1" : "value1"}
In which case you can change the for loop into this
for(var i in cookies){
cookie_names[count] = i;
cookie_values[count] = cookies[i];
count++;
}

Loop through all defined variables jquery

Hi I want to loop through all defined variables in a jquery function for pushing corresponding variable name into an array. The code is given below
function pushallvariables()
{
var list = [];
var name = /^[A-Za-z\s.]+$/;
var general = /^[A-Za-z0-9\s.\-\/]{2,20}$/;
var email = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
var digit = /^[+]?[0-9\s]+$/;
list.push('name');
list.push('general');
list.push('email');
list.push('digit');
}
I want to modify this function into
function pushallvariables()
{
var list = [];
var name = /^[A-Za-z\s.]+$/;
var general = /^[A-Za-z0-9\s.\-\/]{2,20}$/;
var email = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
var digit = /^[+]?[0-9\s]+$/;
for each(variable as var)
{
list.push(var.name);
}
}
But the modified function is not correct. How can I write the function ?
The best and clear way to do that is:
var list = {};
list.name = /^[A-Za-z\s.]+$/;
list.general = /^[A-Za-z0-9\s.\-\/]{2,20}$/;
list.email = /^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
list.digit = /^[+]?[0-9\s]+$/;
without loops, etc

Change formatting in Javascript

Hello i am currently running a function in my javascript called 'save' which has the functionality of:
function save(){
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
var newItem = {};
var num = document.getElementById("num").value;
newItem[num] = {
"methv": document.getElementById("methv").value
,'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
and currently the format of this comes out like this:
[{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}]
is there any way i can change this to a format to something like:
{1173627548,dont know, -,-,U,-,}
Thanks
All you have to do is use an array instead of an object:
newItem = [
num,
document.getElementById('methv').value,
document.getElementById('q1').value,
document.getElementById('q2').value,
document.getElementById('q3').value,
document.getElementById('q4').value,
document.getElementById('comm').value
];

javascript fails when populating array

When I run the following javascript, it fails when input_array[input_array.length] = id; is not commented out. Can anyone see what is causing this?
function cat_images_low_lag () {
var input_array = new array ();
cat_images = $(".category-description").next().find("img");
cat_images.each(function () {
url = $(this).parent().attr("href");
id = url.split("id=");
id = id[1];
input_array[input_array.length] = id;
});
alert ("trst");
alert(input_array.join("\n"));
}
cheers!
First thing, replace:
var input_array = new array ();
With:
var input_array = new Array();
And use this to insert:
input_array.push(id);
Or directly add:
input_array[input_array.length] = id;
Other ways to initialize arrays:
var input_array = [];
Others noted the capitalization problem, but since you're using jQuery, a nicer way to build the Array is like this:
function cat_images_low_lag () {
var input_array = $(".category-description + * img").map(function () {
return this.parentNode.href.split("id=")[1];
}).toArray();
alert ("trst");
alert(input_array.join("\n"));
}
Your initialization of the array is incorrect
var input_array = new array ();
You should use the shorthand
var input_array = [];
or
var input_array = new Array();
Moreover, to avoid having cat_images be a variable in the global scope, you may want to consider scoping it locally like this
var cat_images = $(".category-description").next().find("img");

Categories