Javascript I can take Filelist but I cant take file - javascript

I want take file from filelist but I cant take. How can I make
FileList (inputs[key].files)
FileList (inputs[key].files[0])
var inputs;
inputs = document.getElementsByTagName('input');
inputs = Array.prototype.slice.call(inputs);
var buttons = document.getElementsByTagName("button");
var buttonsCount = buttons.length;
for (var i = 0; i <= buttonsCount; i += 1) {
buttons[i].onclick = function(e) {
var userid = document.querySelector('.userid').innerHTML;
var raffleid = this.id;
var finish = this.value;
if (finish) {
postFinish(userid, raffleid, finish)
}
for (var key in inputs) {
var value = inputs[key].value;
var file = inputs[key].files;
var name = inputs[key].name;
if (value) {
console.log('file: ', file)
postData(raffleid, name, value, userid, file)
}
}
};
}

If I understand correctly, your code is working and you just want to access the Object?
Then try this:
const myArray = Object.values(FileList)
This returns an array of Objects, which you could access with
myArray[0]

Related

Delete Duplicate Data using Google Script

I am trying to make a script to automate deleting duplicate data based on column A. This is the current script I am using and it works.
// This scripts works but deleting new data instead of old data
function removeDuplicates() {
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName(sheetName);
var vA = sh.getDataRange().getValues();
var hA = vA[0];
var hObj = {};
hA.forEach(function(e, i) {
hObj[e] = i;
});
var uA = [];
var d = 0;
for (var i = 0; i <= vA.length; i++) {
if (uA.indexOf(vA[i][hObj['key']]) == -1) {
uA.push(vA[i][hObj['key']]);
} else {
//sh.deleteRow(i + 1 - d++);
sh.deleteRow((parseInt(i)+1) - d);
d++;
}
}
};
But this one is deleting the newly added row, what I want to achieve is it should delete the old duplicate row instead. How can I do that?
In else part instead of using i which represent your current row, use the indexOf the row you want to delete. Delete it first and then push the new one into array
// This scripts works but deleting new data instead of old data
function removeDuplicates() {
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName(sheetName);
var vA = sh.getDataRange().getValues();
var hA = vA[0];
var hObj = {};
hA.forEach(function(e, i) {
hObj[e] = i;
});
var uA = [];
var d = 0;
for (var i = 0; i <= vA.length; i++) {
if (uA.indexOf(vA[i][hObj['key']]) == -1) {
uA.push(vA[i][hObj['key']]);
} else {
//sh.deleteRow(i + 1 - d++);
let j = uA.indexOf(vA[i][hObj['key']]);
sh.deleteRow((parseInt(j)+1) - d);
d++;
uA.push(vA[i][hObj['key']]);
}
}
};

reader.onload function cant read value neither call function

im using reader.onload event to get contents of csv file,
problem is the value displays in console.log() but not in DOM i.e via binding
dropped(event: UploadEvent) {
this.files = event.files;
console.log(this.files)
for (const droppedFile of event.files) {
// Is it a file?
if (droppedFile.fileEntry.isFile) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
// Here you can access the real file
console.log(droppedFile.relativePath, file);
const fileToRead = file;
const fileReader = new FileReader();
fileReader.onload = this.onFileLoad;
fileReader.readAsText(fileToRead);
console.log(this.tempval) /// undefined
});
}
}}
and onFileLoad function is as follows
onFileLoad(fileLoadedEvent) {
const textFromFileLoaded = fileLoadedEvent.target.result;
this.csvContent = textFromFileLoaded;
var allTextLines = this.csvContent.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
var lines = [];
for ( var i = 0; i < allTextLines.length; i++) {
// split content based on comma
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
var tarr = [];
for ( var j = 0; j < headers.length; j++) {
tarr.push(data[j]);
}
lines.push(tarr);
}
}this.tempval = linesconsole.log(this.tempval) // printing value
};
unfortunately data inside this.tempval is not accessible anywhere
not in html DOM or inside dropped() funtion. except inside onFileLoad()
im just new to typescript
thanks in advance

Calling variables outside of a function in javascript

I'm new to javascript and firebase. I would just like to ask if there is a chance to call my variables outside of my function?
Here is my sample code:
function gotData (data) {
console.log(data.val())
var promotions = data.val()
var keys = Object.keys(promotions)
console.log(keys)
for (var i = 0; i < keys.length; i++) {
var k = keys[i]
var name = promotions[k].promotionName
var description = promotions[k].description
var validityFrom = promotions[k].validityPeriodFrom
var validityTo = promotions[k].validityPeriodTo
var dateCreated = promotions[k].dateCreated
var dateUpdated = promotions[k].dateUpdated
console.log(name, description, validityFrom, validityTo, dateCreated, dateUpdated)
}
}
Can I call name, description, validityFrom, etc outside of the function gotData? I'll appreciate a help. Thank you in advance! :)
Define variable as global variable
//above function
var name, description, validityFrom, validityTo, dateCreated, dateUpdated;
function gotData (data) {
console.log(data.val())
var promotions = data.val()
var keys = Object.keys(promotions)
console.log(keys)
for (var i = 0; i < keys.length; i++) {
var k = keys[i]
name = promotions[k].promotionName
description = promotions[k].description
validityFrom = promotions[k].validityPeriodFrom
validityTo = promotions[k].validityPeriodTo
dateCreated = promotions[k].dateCreated
dateUpdated = promotions[k].dateUpdated
console.log(name, description, validityFrom, validityTo, dateCreated, dateUpdated)
}
}
// now you can access variables here
console.log(name, description, validityFrom, validityTo, dateCreated, dateUpdated)
EDIT
You can create an array to get all variables as you may have multidimensional data
var objData = [];
function gotData (data) {
console.log(data.val())
var promotions = data.val()
var keys = Object.keys(promotions)
for (var i = 0; i < keys.length; i++) {
var tempObj = [];
var k = keys[i]
tempObj['promotionName'] = promotions[k].promotionName;
tempObj['description'] = promotions[k].description;
tempObj['validityPeriodFrom'] = promotions[k].validityPeriodFrom;
tempObj['validityPeriodTo'] = promotions[k].validityPeriodTo;
tempObj['dateCreated'] = promotions[k].dateCreated;
tempObj['dateUpdated'] = promotions[k].dateUpdated;
objData.push(tempObj);
}
console.log(objData)
}

change JSON response in javascript

I am working on WEB API's, I have received the JSON response from server, NOW I want to change the returned response into different JSON's to show different table and graphs.
this is the response from server
{"data":[{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}]}
and I need to convert this response to look like this :
{"data":[{"id":3663101,"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0","number2":789},{"id":3663101,"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0","number2":789},{"id":3665337,"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0","number2":456},{"id":3665337,"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0","number2":456},{"id":3665337,"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0","number2":456}]}
this is what I have done so far,
var data = new Array();
var str = '{"data":[{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}]}';
var response = JSON.parse(str);
var tableData = response.data;
var dataLength = response.data.length;
for (i = 0; i < dataLength; i++) {
var obj = {};
obj["id"] = tableData[i].id;
for (a = 0; a < tableData[i].lstImeis.length; a++) {
obj["number"] = tableData[i].lstImeis[a].number,
obj["maxDate"] = tableData[i].lstImeis[a].maxDate,
obj["minDate"] = tableData[i].lstImeis[a].minDate
}
obj["number2"] = tableData[i].number2;
data.push(obj);
}
console.log(JSON.stringify(data));
Here is JSFIDDLE
Outer loops works fines but inner loop gives the last updated data, If you look at the given data and changed data, you will see the difference.
Any idea, why its overriding the values in inner for loop and appending only last values into obj?
Your logic is incorrect line
var obj = {};
obj["id"] = tableData[i].id;
should be inside the inner loop. also line
obj["number2"] = tableData[i].number2;
data.push(obj);
should be moved inside the inner loop.
Here is the improved and working code https://jsfiddle.net/dmtfxt35/4/
var response = JSON.parse(str);
var tableData = response.data;
var finalObj = {data:[]};
for (var i in tableData){
for(var a in tableData[i].lstImeis){
var tmpObj = {};
tmpObj['id'] = tableData[i].id;
tmpObj["number"] = tableData[i].lstImeis[a].number;
tmpObj["maxDate"] = tableData[i].lstImeis[a].maxDate;
tmpObj["minDate"] = tableData[i].lstImeis[a].minDate;
tmpObj["number2"] = tableData[i].number2;
finalObj.data.push(tmpObj);
}
}
console.log(JSON.stringify(finalObj));
I hope this helped.
You using the same obj variable while inserting the data to array.
Obj is dict and it got updated on loop.
I have updated the code as per you requirement
<script>
var data = new Array();
var str = '{"msg":"success","code":"200","status":null,"data":[{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}],"draw":0,"limit":0,"recordsFiltered":0,"recordsTotal":0}';
var response = JSON.parse(str);
var tableData = response.data;
var dataLength = response.data.length;
for(i = 0; i < dataLength; i++){
for(a = 0; a < tableData[i].lstImeis.length; a++){
var obj = {};
obj["id"] = tableData[i].id;
obj["number"] = tableData[i].lstImeis[a].number;
obj["maxDate"] = tableData[i].lstImeis[a].maxDate;
obj["minDate"] = tableData[i].lstImeis[a].minDate;
obj["number2"] = tableData[i].number2;
data.push(obj);
}
}
console.log(JSON.stringify(data));
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You can use array#reduce and array#map.
var response = [{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}];
var result = response.reduce(function(res, obj) {
var temp = obj.lstImeis.map(function(o) {
return Object.assign({}, o, {id: obj.id, number2: obj.number2});
});
return res.concat(temp);
},[])
var output = {data: result};
console.log(JSON.stringify(output));
Look at this Fiddle. You were pushing last updated obj data. You had to push obj each time you update it.
for(i = 0; i < dataLength; i++){
for(a = 0; a < tableData[i].lstImeis.length; a++){
var obj = {};
obj["id"] = tableData[i].id;
obj["number"] = tableData[i].lstImeis[a].number;
obj["maxDate"] = tableData[i].lstImeis[a].maxDate;
obj["minDate"] = tableData[i].lstImeis[a].minDate;
obj["number2"] = tableData[i].number2;
data.push(obj);
}
}

Get data out from callback inside query

I am trying to get data out from query inside the query. I tried everything without success.
getvideos() {
var sectionHolder = [];
var ids = [];
var titles = [];
var videos = [];
var challengeCl = Parse.Object.extend("Challenges");
var query = new Parse.Query(challengeCl);
query.find().then((data) => {
for(var key = 0; key < data.length; key++) {
var object = data[key];
var videoid = object.id;
var title = object.get('title');
ids.push(videoid)
titles.push(title)
var video = Parse.Object.extend('Videos');
var queryv = new Parse.Query(video);
queryv.equalTo('challengeid', videoid);
queryv.find().then(
(video) => {
videos.push(video); //i want to get this data out from this scope
});
sectionHolder.push({title: title, data: videos}); // <---- i want to put videos data in data
}
console.log(sectionHolder);
this.setState({listData: sectionHolder});
});
}
I read parse documentation but there is nothing related to this.

Categories

Resources