I wanted to obtain the stock ticker symbol from this data. This is the progress I made so far. Afterwards, I just ended up going in circles. I was trying to get the ticker characters together in an array like this: ['VOO', 'DIS'...]
var data = "VOO2.1315$232.86$23.34$496.34DIS1.94733$96.40$12.09$187.72IWM1.07511$112.56$4.24$121.01DAL5$29.55-$16.70$147.75XOM2$36.95-$29.42$73.90VYM1$69.75-$0.69$69.75SBUX1.19403$66.34$2.82$79.21HAL11$6.39$1.21$70.29SFYX9$7.61-$0.40$68.45O1$56.90$2.07$56.90WPG40$1.03-$44.80$41.20SFY6$9.06$0.00$54.36SCHA1$50.96$2.22$50.96DIG10$4.58-$5.40$45.80MO1$36.64-$2.73$36.64DVN6$6.29-$4.26$37.74CSCO1$38.82$5.99$38.82AEG14$2.62-$17.64$36.68XLE1$28.33-$1.91$28.33DOW1$28.56$5.31$28.56RDSA1$31.64$5.46$31.64CBL50$0.24-$13.86$12.14";
function tickerSep(info){
var result = [];
for (var i = 0; i < info.length; i++){
if (info[i].match(/([A-Z])/g)) {
result.push(info[i]);
}
}
console.log(result);
}
tickerSep(data);
The regex g flag will do that for you.
function tickerSep(info) {
return info.match(/[A-Z]+/g);
}
Read more about it here.
this should do the trick. Let me know if you don't understand.
var data = "VOO2.1315$232.86$23.34$496.34DIS1.94733$96.40$12.09$187.72IWM1.07511$112.56$4.24$121.01DAL5$29.55-$16.70$147.75XOM2$36.95-$29.42$73.90VYM1$69.75-$0.69$69.75SBUX1.19403$66.34$2.82$79.21HAL11$6.39$1.21$70.29SFYX9$7.61-$0.40$68.45O1$56.90$2.07$56.90WPG40$1.03-$44.80$41.20SFY6$9.06$0.00$54.36SCHA1$50.96$2.22$50.96DIG10$4.58-$5.40$45.80MO1$36.64-$2.73$36.64DVN6$6.29-$4.26$37.74CSCO1$38.82$5.99$38.82AEG14$2.62-$17.64$36.68XLE1$28.33-$1.91$28.33DOW1$28.56$5.31$28.56RDSA1$31.64$5.46$31.64CBL50$0.24-$13.86$12.14";
function tickerSep(info){
var result = [];
var temp = '';
var myArray = data.split('$');
var len = myArray.length;
for (var i = 0; i < len; i++){
temp ='';
for(var j = 0;j<myArray[i].length;j++){
if (myArray[i][j].match(/([A-Z])/g)) {
temp =temp + myArray[i][j];
}
}
if(temp != '')result.push(temp);
}
console.log(result);
}
tickerSep(data);
function tickerSep1(info) {
return info.match(/[A-Z]+/g);
}
console.log(tickerSep1(data));
Related
I am trying to write a custom function that will return an array of all of the unique values when two arrays are compared. This is what I have and it is not working:
function getUniqueCells(range1, range2) {
var sheet = SpreadsheetApp.getActiveSheet();
var range1 = sheet.getRange(range1);
var range2 = sheet.getRange(range2);
var range1Val = range1.getValues();
var range2Val = range2.getValues();
var uniquesArr = [];
for (var i = 0; i <= range1Val.length; i++) {
for(var u = 0; u <= range2Val.length; i++){
if(range1Val[i] === range2Val[u]) {
break;
} else if((u + 1) === range2Val.length){
uniquesArr.push(range1Val[i]);
};
};
};
return uniquesArr;
}
Is there a way not to get the "Internal error executing the custom function" error?
there is an issue on your second for loop.
for(var u = 0; u <= range2Val.length; i++){
it should be for(var u = 0; u <= range2Val.length; u++){
You cant check by range1Val[i] === range2Val[u]. Because they are arrays.
I had the same problem. found answer here:
compare rows on google spreadsheets
I've looked around for some help on this topic but was unable to find some help or guidance.
My problem is I am attempting to perform a sort on a series of values separated by an equals sign.
"Foo=Bar , Shenanigans=Fun, A=B ...etc"
My current sort works, but only if no value is the same. If I have some values like:
"Foo=Bar, A=Bar, Potato=Bar"
When the sort is complete they will all be "A=Bar"
My current sort looks like this, would someone be able to point me in the right direction?
$('#sortByValue').click(function() {
var textValueArray = document.getElementById('nameValuePairList');
textArray = new Array();
valueArray = new Array();
oldValues = new Array();
for (i = 0; i < textValueArray.length; i++) {
valueArray[i] = textValueArray.options[i].value;
textArray[i] = textValueArray.options[i].text;
oldValues[i] = textValueArray.options[i].value;
}
valueArray.sort(function(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
for (i = 0; i < textValueArray.length; i++) {
textValueArray.options[i].value = valueArray[i];
for (j = 0; j < textValueArray.length; j++) {
if (valueArray[i] == oldValues[j]) {
textValueArray.options[i].text = textArray[j];
j = textValueArray.length;
}
}
}
});
I know that my problem lies here: valueArray[i] == oldValues[j]
as when the data comes in valueArray = {Bar, Foo, Bar} while textArray = {Foo=Bar, A=Foo, Test=Bar}
However, I am unsure how to best resolve it.
Sort textArray directly, don't use valueArray since it will contain duplicates:
textArray.sort(function(a,b){
var aa = a.split('=')
var bb = b.split('=')
var a_key = aa[0].toLowerCase(), a_val = aa[1].toLowerCase();
var b_key = bb[0].toLowerCase(), b_val = bb[1].toLowerCase();
if (a_val == b_val) return a_key.localeCompare(b_key);
return a_val.localeCompare(b_val);
})
I would do something like this:
document.getElementById('sortByName').onclick = sortByName;
function sortByName(){
var myList = document.getElementById('list');
var values = [];
for (var i=0;i<myList.options.length;i++) {
values[i] = myList.options[i].text;
}
values.sort(function (a, b){
if(a !== "" && b !== ""){
return a.split('=')[0].localeCompare(b.split('=')[0]);
} else {
return 0;
}
});
clearList(myList);
fillList(myList, values);
}
function clearList(list) {
while (list.options.length > 0) {
list.options[0] = null;
}
}
function fillList(myList, values){
for (var i=0;i<values.length;i++) {
var option = document.createElement("option");
option.text = values[i];
myList.options[i] = option;
}
}
Take a look at this demo
The reasoning behind doing this at all will have you wondering why, in the future. I think you want something like this:
function inArray(v, a){
for(var i=0,l=a.length; i<l; i++){
if(a[i] === v){
return true;
}
}
return false;
}
function sortWeirdString(str){
var pairs = str.split(/\s?,\s?/), n = [], v = [], c = [], ci, idx = [], cl, nv = [], ra = [];
for(var i=0,l=pairs.length; i<l; i++){
var pair = pairs[i].split(/\s?=\s?/);
n.push(pair[0]); v.push(pair[1]);
}
c = n.concat().sort(); cl = c.length
for(var i=0; i<cl; i++){
var cv = c[i];
if(n.indexOf){
ci = n.indexOf(cv);
if(inArray(ci, idx)){
ci = n.indexOf(cv, ci+1);
}
idx.push(ci);
}
else{
for(var x=0; x<cl; x++){
if(n[x] === cv){
if(inArray(x, idx)){
continue;
}
idx.push(x);
}
}
}
}
for(var i=0,l=idx.length; i<l; i++){
ra.push(c[i]+'='+v[idx[i]]);
}
return ra.join(', ');
}
$('#sortByValue').click(function(){
console.log(sortWeirdString($('#nameValuePairList').val()));
}
Update 2019
The spec has changed and #Array.prototype.sort is now a stable sort.
The elements of this array are sorted. The sort must be stable (that
is, elements that compare equal must remain in their original order)
This is already implemented in V8
I have the following data array:
var ans =
[
{"text":"x","response":false},
{"text":"y","response":false},
{"text":"z","response":true}
];
var correct = "010"; // I need to add this to the array ans
Can anyone suggest how I could use use the data in the correct variable to add to the array so as to make:
var ans =
[
{"text":"x","response":false,"correct":false},
{"text":"y","response":false,"correct":true},
{"text":"z","response":true,"correct":false}
];
for(var i=0; i< ans.length; i++) {
ans[i].correct = correct.charAt(i) == "1";
}
for (var i = 0; i < correct.length; i++) {
ans[i]["correct"] = correct[i] === "1";
}
You an also do it like this(using a for-in loop).
Reference: For-each over an array in JavaScript?
for(key in ans){
ans[key].correct = correct.charAt(key) == "1";
}
var ans =
[
{"text":"x","response":false},
{"text":"y","response":false},
{"text":"z","response":true}
];
var correct = "010";
var sep = correct.split("");
var arr = [];
for (var i = 0; i < sep.length; i++) {
if (sep[i] == 0) {
arr.push("false");
} else {
arr.push("true");
}
}
var len = ans.length;
for (var i = 0; i < len; i++) {
ans[i].correct = arr[i];
}
Hi I'm trying to split a string based on multiple delimiters.Below is the code
var data="- This, a sample string.";
var delimiters=[" ",".","-",","];
var myArray = new Array();
for(var i=0;i<delimiters.length;i++)
{
if(myArray == ''){
myArray = data.split(delimiters[i])
}
else
{
for(var j=0;j<myArray.length;j++){
var tempArray = myArray[j].split(delimiters[i]);
if(tempArray.length != 1){
myArray.splice(j,1);
var myArray = myArray.concat(tempArray);
}
}
}
}
console.log("info","String split using delimiters is - "+ myArray);
Below is the output that i get
a,sample,string,,,,This,
The output that i should get is
This
a
sample
string
I'm stuck here dont know where i am going wrong.Any help will be much appreciated.
You could pass a regexp into data.split() as described here.
I'm not great with regexp but in this case something like this would work:
var tempArr = [];
myArray = data.split(/,|-| |\./);
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] !== "") {
tempArr.push(myArray[i]);
}
}
myArray = tempArr;
console.log(myArray);
I'm sure there's probably a way to discard empty strings from the array in the regexp without needing a loop but I don't know it - hopefully a helpful start though.
Here you go:
var data = ["- This, a sample string."];
var delimiters=[" ",".","-",","];
for (var i=0; i < delimiters.length; i++) {
var tmpArr = [];
for (var j = 0; j < data.length; j++) {
var parts = data[j].split(delimiters[i]);
for (var k = 0; k < parts.length; k++) {
if (parts[k]) {
tmpArr.push(parts[k]);
}
};
}
data = tmpArr;
}
console.log("info","String split using delimiters is - ", data);
Check for string length > 0 before doing a concat , and not != 1.
Zero length strings are getting appended to your array.
I'm looping through a set of inputs. I need to tally up the grouped totals.
var compoundedArray = new Array();
holder.find(".dataset input").each(function(index) {
var val = $(this).val();
var dataType = $(this).data("type");
var localObj = {};
localObj[dataType] = val;
compoundedArray.push(localObj);
});
I have an object like this
[
{
"growth":30
},
{
"growth": 40
},
{
"other": 20
}
]
how do I loop through the object to produce something like
[
{
"growth": 70
},
{
"other": 20
}
]
if I looped over the initial array object
for (var i = 0; i < compoundedArray.length; i++) {
console.log(compoundedArray[i]);
}
how would I go about checking to ensure I don't have duplicates - and that I can tally up the results?
I think your selection of data structure is a bit too complicated. Try something like.
var compoundedObject = {};
holder.find(".dataset input").each(function(index) {
var val = $(this).val();
var dataType = $(this).data("type");
//Assuming all values are integers and can be summed:
if( compoundedObject.hasOwnProperty(dataType) )
{
compoundedObject[dataType] += val;
}
else
{
compoundedObject[dataType] = val;
}
});
You will end up with an object, not an array though.
var add=function (a,b){ a=a||0; b=b||0; return a+b};
var input=[ {growth:30},{growth:40},{other:20} ],output=[],temp={};
$.each(input,function(i,o){
var n;
for(i in o)
{n=i;break}
temp[n]=add(temp[n],o[n]);
});
$.each(temp,function(i,o){
var k={};
k[i]=o;
output.push(k)
});
find output at output variable.
Do not post much specific question, It might not help others.
This works. And it's pure javascript.
var totals = {};
for (var i = 0; i < compoundedArray.length; i++) {
var item = compoundedArray[i];
for (var key in item) {
totals[key] = (totals[key] || 0) + item[key]
}
};
You can loop trough an Object with a for loop.
If you want to delete an item simply set it to null.
Example:
for(var i in compoundedArray){
for(var j in compoundedArray){
if(i == j){
compoundedArray[i] += compoundedArray[j];
compoundedArray[j] = null;
}
}
}
You can do the following:
var totals = [], tmp = {};
for (var i = 0; i < compoundedArray.length; i++) {
var obj = compoundedArray[i];
for (var j in obj) {
tmp[j] = tmp[j] || 0;
tmp[j] += obj[j];
}
}
for(var k in tmp) {
var obj = {};
obj[k] = tmp[k];
totals.push(obj);
}
See this working demo