I am writing a script to use on a Google Sheet where I am accepting user input and comparing it to the values already present on the spreadsheet.
In the function below I have retrieved the users desired number with the variable voltReqed.
I am attempting to compare it (detect less than or greater than) to each value in the array generated with the variable voltCompareData.
I am quite new to scripts, please excuse my ignorance.
function getVoltage() {
var voltReqed = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("A1").getValue();
Logger.log(voltReqed);
var voltCompareData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SpecsList").getSheetValues(3, 2, 1, 3);
Logger.log(voltCompareData);
var voltSample1 = voltCompareData.getValue(1,1);
Logger.log(voltSample1);
}
The variable voltSample1 was me attempting to pull the first number out of the array generated by the voltCompareData variable for comparison operations.
Please see the image for the number and array retrieved and logged by the above script.
Comparing data to variable
function getVoltage() {
var voltReqed = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("A1").getValue();
var vs = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SpecsList").getSheetValues(3, 2, 1, 3).flat();
vs.forEach(e => {
if(e == v) {
//do someting
}
});
}
I'm parsing an order feed to identify duplicate items bought and group them with a quantity for upload. However, when I try to map the resulting array, it's showing [object Object], which makes me think something's converting the return into an object rather than an array.
The function is as follows:
function compressedOrder (original) {
var compressed = [];
// make a copy of the input array
// first loop goes over every element
for (var i = 0; i < original.length; i++) {
var myCount = 1;
var a = new Object();
// loop over every element in the copy and see if it's the same
for (var w = i+1; w < original.length; w++) {
if (original[w] && original[i]) {
if (original[i].sku == original[w].sku) {
// increase amount of times duplicate is found
myCount++;
delete original[w];
}
}
}
if (original[i]) {
a.sku = original[i].sku;
a.price = original[i].price;
a.qtty = myCount;
compressed.push(a);
}
}
return compressed;
}
And the JS code calling that function is:
contents: compressedOrder(item.lineItems).map(indiv => ({
"id": indiv.sku,
"price": indiv.price,
"quantity": indiv.qtty
}))
The result is:
contents: [ [Object], [Object], [Object], [Object] ]
When I JSON.stringify() the output, I can see that it's pulling the correct info from the function, but I can't figure out how to get the calling function to pull it as an array that can then be mapped rather than as an object.
The correct output, which sits within a much larger feed that gets uploaded, should look like this:
contents:
[{"id":"sku1","price":17.50,"quantity":2},{"id":"sku2","price":27.30,"quantity":3}]
{It's probably something dead simple and obvious, but I've been breaking my head over this (much larger) programme till 4am this morning, so my head's probably not in the right place}
Turns out the code was correct all along, but I was running into a limitation of the console itself. I was able to verify this by simply working with the hard-coded values, and then querying the nested array separately.
Thanks anyway for your help and input everyone.
contents: compressedOrder(item.lineItems).map(indiv => ({
"id": indiv.sku,
"price": indiv.price,
"quantity": indiv.qtty
}))
In the code above the compressedOrder fucntion returns an array of objects where each object has sku, price and qtty attribute.
Further you are using a map on this array and returning an object again which has attributes id, price and quantity.
What do you expect from this.
Not sure what exactly solution you need but I've read your question and the comments, It looks like you need array of arrays as response.
So If I've understood your requirement correctly and you could use lodash then following piece of code might help you:
const _ = require('lodash');
const resp = [{key1:"value1"}, {key2:"value2"}].map(t => _.pairs(t));
console.log(resp);
P.S. It is assumed that compressedOrder response looks like array of objects.
Im trying to make pseudo-random sequence generator that works just line Linear Feedback Shift Register.
Im doing it in JavaScript because its the only language that I know and im using HTML to create GUI.
User should type in initial value and get schematic diagram and pseudo-random sequence itself.
Here is my JavaScript code:
var UserInput = document.getElementById('ulaz');
var Output = document.getElementById('izlaz');
//variable `data` is an array of objects which I used to store pictures of circuits
// and [taps][3] necessary for shift registers to give max possible length output
// before going into loop which is 2^n-1, where n (`bit` in my code) is number of
//register blocks and number of digits in input value.
function pss (){
var data = [
{
slika:"pic/2bit.png",
tap:[0,1]
},
{
slika:"pic/3bit.png",
tap:[0,2]
},
{
slika:"pic/4bit.png",
tap:[0,3]
},
{
slika:"pic/5bit.png",
tap:[1,4]
},
{
slika:"pic/6bit.png",
tap:[0,5]
},
{
slika:"pic/7bit.png",
tap:[0,6]
},
{
slika:"pic/8bit.png",
tap:[1,2,3,7]
},
{
slika:"pic/9bit.png",
tap:[3,8]
},
{
slika:"pic/10bit.png",
tap:[2,9]
},
{
slika:"pic/11bit.png",
tap:[1,10]
},
{
slika:"pic/12bit.png",
tap:[0,3,5,11]
},
{
slika:"pic/13bit.png",
tap:[0,2,3,12]
},
{
slika:"pic/14bit.png",
tap:[0,2,4,13]
},
{
slika:"pic/15bit.png",
tap:[0,14]
},
{
slika:"pic/16bit.png",
tap:[1,2,4,15]
},
{
slika:"pic/17bit.png",
tap:[2,16]
},
{
slika:"pic/18bit.png",
tap:[6,17]
},
{
slika:"pic/19bit.png",
tap:[0,1,4,18]
},
{
slika:"pic/20bit.png",
tap:[2,19]
},
{
slika:"pic/21bit.png",
tap:[1,20]
},
{
slika:"pic/22bit.png",
tap:[0,21]
},
{
slika:"pic/23bit.png",
tap:[4,22]
},
{
slika:"pic/24bit.png",
tap:[0,2,3,23]
},
{
slika:"pic/25bit.png",
tap:[2,24]
},
{
slika:"pic/26bit.png",
tap:[0,1,5,25]
},
{
slika:"pic/27bit.png",
tap:[0,1,4,26]
},
{
slika:"pic/28bit.png",
tap:[2,27]
},
{
slika:"pic/29bit.png",
tap:[0,28]
},
{
slika:"pic/30bit.png",
tap:[0,3,5,29]
},
{
slika:"pic/31bit.png",
tap:[2,30]
},
{
slika:"pic/32bit.png",
tap:[1,5,6,31]
}
];
var first = UserInput.value.split("");
for (k=0;k<first.length;k++) first[k] = +first[k];
//first is just UserInput separated in one char strings than parsed to integers
var bit = first.length - 2;
// I subtracted 2 here so I can access objects from data
var matrix = [first];
var t = 0;
var between;
var z;
for (i=1; i<Math.pow(2, bit+2)-1; i++){ //here is that 2^n-1. +2 is because i had -2 before. For loop is starting from 1 and ending with <2^n-1 because i already have first array of matrix
for (j=0; j<data[bit].tap.length; j++){
z = data[bit].tap[j];
t = t ^ matrix[i-1][z];
} // this for makes "t" which is all taps XOR-ed. If user input was 101, tap would be [0,2] and t would be 1xor1=0
between = matrix[i-1];
console.log(between);
between.unshift(t);
between.pop();
matrix[i] = between;
t=0; // here Im "shifting registers" or just placing t in front of last generated row and removing its last digit, thus generating new row
}
console.log(matrix);
}
and here is HTML so you can run it.
variable data is an array of objects which I used to store pictures of circuits and taps necessary for shift registers to give max possible length output before going into loop which is 2^n-1, where n (bit in my code) is number of register blocks and number of digits in input value.
So problem is: console.log(between); which logs last generated row is all correct except, ofc, there is no last row because it shows last generated, but than console.log(matrix) which should log complete matrix , shows all rows overwritten by last one.
So for user input 101, matrix should be
101
010
001
100
110
111
011
but is just
011
011
011 ...
I cant figure out what is wrong with it if part before console.log(between); is all fine...
P.S. Code is not finished it wont display solution in HTML, and there still needs to be part of function that makes an array from last column of matrix, which is pseudo-random sequence.
I realized that var between refers to the same array as var matrix[i-1], rather than a new, independent array.
between = matrix[i-1];
So, if you want to store only values of matrix[i-1], not to create reference, you can do this like this:
between = JSON.parse(JSON.stringify(matrix[i-1]));
In js when you copy array in some variable, you create reference of that array by default. There are many ways to avoid this, and you can find many examples here.
I do not know why but I've come to a solution (will investigate more when I get free time).
for (i=1; i<Math.pow(2, bit+2)-1; i++){ //here is that 2^n-1. +2 is because i had -2 before. For loop is starting from 1 and ending with <2^n-1 because i already have first array of matrix
for (j=0; j<data[bit].tap.length; j++){
z = data[bit].tap[j];
t = t ^ matrix[i-1][z];
} // this for makes "t" which is all taps XOR-ed. If user input was 101, tap would be [0,2] and t would be 1xor1=0
between = matrix[i-1];
console.log(between);
between.unshift(t);
between.pop();
// MODIFICATION
var between_string = between;
matrix[i] = between_string.join(); // Turn it to a string
matrix[i] = matrix[i].split(','); // Turn it back to array to keep it working on the for loop above.
// END MODIFICATION
t=0; // here Im "shifting registers" or just placing t in front of last generated row and removing its last digit, thus generating new row
}
Now, when you print it out in the console it'll show you a bidimentional array, although it's weird, sometimes (on my console) it shows int numbers and sometimes mixed with string numbers (respecting the original value of between).
Edit: I tried only using "101" for the input.
Second edit: Okay, I feel ashamed, the reason why it returns [1, "0", "0"] (example) is because of split(',') for "1,0,0"(only tow numbers were preceded by comas). Haha. Sorry.
I have a csv data.
DATA :
Time,Count
1377973800,293
1377975600,212
1377977400,129
1377979200,89
1377981000,54
1377982800,21
1377984600,15
I want to return the data in this format.
{
"946705035":4,
"946706692":4,
"946707210":0,
"946709243":2,
"946710714":5,
"946712907":3,
"946713183":4,
"946719001":0
}
I do not want the header Time and Count to be appeared in the json format.
Tried using d3.nest() but the result I got like it starts with key variable which i don't want.
Someone please help me in getting the data in that format.
I believe a code similar to this would do the job:
d3.csv("data.csv", function(error, data) {
var myObject = {};
for (var i=0; i < data.length; i++)
myObject[data[i].Time] = data[i].Count;
};
This gives you data about counts as strings, and if you want numbers, you can just add a "+", which will trigger conversion from string to number:
myObject[data[i].Time] = +data[i].Count;
EDIT: Here is related question on creating object properties dynamically, maybe you can find something useful there too.
this question should be quite simple, but I haven't been able to figure it out.
I want to collect all the VELatLong objects iterating through a list of polygons in JavaScript in order to use the SetMapView() method.
So far I have been able to do with just 2 polygons and my code looks like this:
var points = [];
// Getting the points for first polygon
map.AddShape(shapeOne);
points = shape.GetPoints();
// Getting the points for second polygon and Concatenating "points" with "pointsTwo".
map.AddShape(shapeTwo);
pointsTwo = shape.GetPoints();
points.concat(pointsTwo);
map.SetMapView(points);
But I would like help to how I can do the same thing iterating through a list of polygons?
My iteration code works fine, it looks like this:
function btnPolygons_Click()
{
$.post
(
"/Search/GetPolygons",
null,
function (items) {
$.each
(
items,
function (i, polygonItem) {
var wktShape = polygonItem.PolygonWKT
// Create a VEShape from the WKT representation
var shape = VirtualEarthWKT.ShapeFromWKT(wktShape);
// Add VEShape to Map
map.AddShape(shape);
}
);
},
"json"
);
}
Can you tell me what to add to my iteration code in order to collect all the VELatLong objects iterating through the list of polygons?
This solved my problem:
// Getting the points of the first iteration.
if (i == 0) {
points = shape.GetPoints();
}
// Concatenating the points of the first iteration to the following iterations.
else {
pointsTwo = shape.GetPoints();
points = points.concat(pointsTwo);
}
// Setting the map view in the last iteration.
if (i == items.length - 1) {
map.SetMapView(points);
}