Parsing an array of objects and pushing selectively to new object - javascript

I am trying to parse a JSON response from transloadit and save off the result objects in groups to store separately.
This is the JSON: https://jsonblob.com/552917cee4b0237a964c0de1
I am trying this...
var results = response.results;
var versions = {};
for (var index in results) {
var this_key = index;
for (var i = 0; i < results[index].length; i++) {
if(results[index][i].md5hash === media.md5){
versions[this_key] += results[index][i];
}
}
}
But when I console versions I am only getting
{":original":"undefined[object Object]"}
Whereas I would have expected something closer to what I want to achieve which is
{":original":[object Object],"l":[object Object]}
The intention is to insert this into MongoDB so that I am left with
"versions":{"l":{"name":"foo"...},"m":{"name":"bar"...}}

The issue you are facing is because of the versions[this_key] not initialized.
Check http://jsfiddle.net/Lhzgc7tq/
var results = response.results;
var versions = {};
for (var index in results) {
var this_key = index;
for (var i = 0; i < results[index].length; i++) {
if(results[index][i].md5hash === media.md5){
versions[this_key] = "";
versions[this_key] += results[index][i];
}
}
}
console.log(versions);

Related

How to obtain stock ticker symbol from this data in JavaScript?

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));

Push array inside another array in js?

I have a multidimensional array in js that I want to fill using a for bucle.
To do this, I use this code:
var array = [];
var aux = [];
for(i=0;i<10;i++){
aux = [i,i+1,i+2];
array.push({
name:i,
data:aux
});
}
The issue is that the name is changed but the data key is overwritten, so I think that I have to push the data too, but don't know how.
Maybe something like that
const array = [];
for (i = 0; i < 10; i++) {
const aux = [];
const quantity = i + 3;
for (j = i; j < quantity; j++) {
aux.push(j);
}
array.push({ name: i, data: aux });
}
Let me know if need some help or change

Google Sheet Script Editor: only logging first character of key in array of objects

I am trying to loop through an array of objects using Script Editor in Google Sheets. However, when I log the variable 'temp', it only returns the first letter of the first key of the first object in the array. I tried converting everything to a string using the TO_TEXT function, but it produced the same result.
I've looked through Google forums and can not find anything that pointed to a solution to this issue. Any help would be much appreciated.
The purpose of this project is to loop through a set data and identify if a particular action occurred that does not have a corresponding action. For instance, if a user viewed something, but did not also update that same thing, I want to push the object where the action was solely viewing into a separate array.
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Security Risk Audit - users')
.addItem('Perform user audit', 'getSheetData')
.addToUi();
}
var sheet = SpreadsheetApp.getActiveSheet();
var allSheetData = sheet.getDataRange().getValues();
var fileName = SpreadsheetApp.getActive().getName();
var headerRow = allSheetData[0];
var data = [];
var filteredData = [];
function getSheetData (){
for (var currentRow = 1; currentRow < allSheetData.length; currentRow++){
var obj = {};
for (var rowColumn = 0; rowColumn<headerRow.length; rowColumn++){
obj[headerRow[rowColumn]]=allSheetData[currentRow][rowColumn];
}
data.push(obj);
}
filterData();
}
function filterData() {
for (var i = 0; i < data.length; i++){
var temp = data[i].patientid[i];
Logger.log(temp);
for(var j = 0; j<data.length; j++) {
if(temp == data[j].patientid[j] && data[j].type[j] == "view"){
var temp2 = data[j].patientid[j];
} else {
return false;
}
for (var k = 0; k<data.length;k++){
if(temp2 == data[k].patientid[k] && data[k].type[k] != "view") {
return false;
}else{
filteredData.push(data[k]);
}
}
}
}
return filteredData;
}

Javascript array not getting modified

I have some JSON data in the "data" variable.
FORMAT :
{
"0" : {"names":"Pooja, Trivedi"},
"1" : {"names":"Pooja, Rooster"}
}
I need to implement a map so as to count the different names:
Pooja = 2
Trivedi = 1
Rooster = 1
Here is the implementation:
map = new Array();
data = jQuery.parseJSON(data); //convert JSON to object
for (var i = 0; i < data.length; i++) {
var names = data[i].names.split(','); //split the CSVs
for (var j = 0; j < names.length; j++) {
if (map[names[j].trim()] === undefined) {
map[names[j].trim()] = 1;
} else {
map[names[j].trim()]++;
}
}
console.log(map); //shows progressively filled arrays
}
console.log(map); //shows [], an empty array
Inside the loop, the map is updated.
However after the end of the i loop, all we have is an empty array.
How is this to be resolved?
First of all, you don't want to use for (var i = 0; i < data.length; i++) to traverse your data object, because a Javascript object doesn't have a length property like an array would. So, use a for (var i in data) instead, to traverse all the keys in the object.
This works:
var data = {
"0" : {"names":"Pooja, Trivedi"},
"1" : {"names":"Pooja, Rooster"}
}
var map = {};
for (var i in data) {
var names = data[i].names.split(',');
for (var j in names) {
var name = names[j].trim();
if (map[name]) {
map[name]++;
} else {
map[name] = 1;
}
}
console.log(map); //shows progressively filled arrays
}
console.log(map); //shows the full array
And you don't want to use an array as your map. You should instead use an object.
I like this version better:
var data = {
"0" : {"names":"Pooja, Trivedi"},
"1" : {"names":"Pooja, Rooster"}
};
var nameCounts = {};
for (var item in data) {
data[item].names.split(", ").forEach(function (name) {
if (name in nameCounts) {
nameCounts[name]++;
} else {
nameCounts[name] = 1;
}
});
}
console.log(nameCounts);

Copying array of objects into another array is it reference type in JQuery

I have a array of objects TransactionVModel.FiltersList[].
When I copy this array to another array fltrList[] and if I modify any of the object in array fltrList will it get reflect in Array TransactionVModel.FiltersList in JQuery ? For better clarity below is my example. As per me since it is a reference type it should update array TransactionVModel.FiltersList as well but in my scenario it is not happening, can I know why it is not happening ?
TransactionVModel.FiltersList is declared as ko.observableArray(); in my code.
function UpdateSelectedFilters(data) {
var fltrList = [];
fltrList = TransactionVModel.FiltersList();
for (var i = 0; i < data.length ; i++) {
var index = fltrList.indexOf(data[i]);
if (index != -1) {
var fltrObj = fltrList[index];
var fltrValArr = [];
fltrValArr = data.valueItems;
for (var j = 0; j < fltrValArr.length; j++) {
if (fltrValArr[j].IsSelected == true) {
if (fltrObj.indexOf(fltrValArr[j]) != -1) {
var selectedVal = fltrObj[fltrObj.indexOf(fltrValArr[j])];
selectedVal.IsSelected = true;
}
}
}
}
}
In my scenario I am updating selectedVal.IsSelected property but it is not reflecting the observableArray TransactionVModel.FiltersList.
You need to tell knockout that your array has changed with valueHasMutated:
function UpdateSelectedFilters(data) {
var fltrList = [];
fltrList = TransactionVModel.FiltersList();
//...
TransactionVModel.FiltersList.valueHasMutated();
}

Categories

Resources