javascript, keep getting undefined in loop - javascript

I have this code, but for some reason I keep getting colours.undefined
var varray = new Array("WH", "BL");
var fname = "colours",
lname = "size_code_id";
var arr = new Array();
var mquery = {
"$or": []
};
for (var i = 0; i < varray.length; i++); {
var query = {
"$and": []
};
var s1 = fname.concat(".").concat(varray[i]);
var s2 = fname.concat(".").concat(varray[i]).concat(".").concat(lname);
var sub1 = {}, sub2 = {};
sub1[s1] = {
$exists: 1
};
sub2[s2] = "S";
query["$and"].push(sub1);
query["$and"].push(sub2);
arr.push(query);
};
I want to get colours.WH.size_code_id but keep getting colours.undefined.size_code_id
What am i missing, any advice much appreciated

Remove ; after
for (var i = 0; i < varray.length; i++) ; {
Example
var varray = new Array("WH", "BL");
var fname = "colours",
lname = "size_code_id";
var arr = new Array();
var mquery = {
"$or": []
};
for (var i = 0; i < varray.length; i++) {
var query = {
"$and": []
};
var s1 = fname.concat(".").concat(varray[i]);
var s2 = fname.concat(".").concat(varray[i]).concat(".").concat(lname);
var sub1 = {}, sub2 = {};
sub1[s1] = {
$exists: 1
};
sub2[s2] = "S";
query["$and"].push(sub1);
query["$and"].push(sub2);
arr.push(query);
};
console.log(arr)

Related

Changing My working script to Dockable script does not work as expected

I have a working script for after effects, but when I change it to Dockable script,it wont work. Please help me to figure out why? If I take it out of the dock script it is working. I copied this dockable code from goodboy.ninja website.
This the final Dockable Code. Where is the mistake?
(
function(thisObj) {
buildUI(thisObj);
‍
function buildUI(thisObj) {
var windowName = "Diffrence Checker";
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("window", windowName, undefined, {
resizeable: true
});
// Dropdown list
var groupOne = myPanel.add("group", undefined, "groupOne");
groupOne.orientation = "row";
groupOne.add("StaticText", undefined, "Main Comp");
var mainDD = groupOne.add("dropdownlist", undefined, compArrayNames);
mainDD.size = [175, 25];
var groupTwo = myPanel.add("group", undefined, "groupTwo");
groupTwo.orientation = "row";
groupTwo.add("StaticText", undefined, "Diff File");
var diffDD = groupTwo.add("dropdownlist", undefined, VideoArrayNames);
diffDD.size = [175, 25];
// Button code
var createBT = myPanel.add("group", undefined, "createBT");
createBT.orientation = "column";
var mainbutton = createBT.add("button", undefined, "Create");
//myPanel.center();
//myPanel.show();
‍
myPanel.onResizing = myPanel.onResize = function() {
this.layout.resize();
};
if (myPanel instanceof Window) {
myPanel.center();
myPanel.show();
} else {
myPanel.layout.layout(true);
myPanel.layout.resize();
}
}
// Write your helper functions here
var compArray = [];
var compArrayNames = [];
var VideoArray = [];
var VideoArrayNames = [];
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i).hasVideo == true && app.project.item(i).file == null) {
compArray.push(app.project.item(i));
compArrayNames.push(app.project.item(i).name);
}
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio == true) {
VideoArray.push(app.project.item(i));
VideoArrayNames.push(app.project.item(i).name);
}
}
function createCP() {
app.beginUndoGroup("Editing");
var comlayer = compArray[mainDD.selection.index];
var vidlayer = VideoArray[diffDD.selection.index];
var getcomp = app.project.items.addComp(VideoArrayNames[diffDD.selection.index] + "_DIFF".toString(), 1920, 1080, 1, comlayer.duration, 30);
getcomp.openInViewer();
addVideofile(comlayer, getcomp);
addvideotwofile(vidlayer);
renderQ();
app.endUndoGroup();
}
function addVideofile(comlayer) {
app.project.activeItem.layers.add(comlayer);
}
function addvideotwofile(vidlayer) {
var newlayer = app.project.activeItem.layers.add(vidlayer);
newlayer.blendingMode = BlendingMode.DIFFERENCE;
}
function renderQ() {
var Qcomp = app.project.activeItem;
var Qitem = app.project.renderQueue.items.add(Qcomp);
}
}
)
(this);
My Original script is as follows:
// Diffrence Check
// global variables
var compArray = [];
var compArrayNames = [];
var VideoArray = [];
var VideoArrayNames = [];
for (var i = 1; i <= app.project.numItems; i++){
if (app.project.item(i).hasVideo == true && app.project.item(i).file == null) {
compArray.push (app.project.item(i));
compArrayNames.push (app.project.item(i).name);
}
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio== true) {
VideoArray.push (app.project.item(i));
VideoArrayNames.push (app.project.item(i).name);
}
}
var mainWindow = new Window("palette","Difference Checker",undefined);
mainWindow.orientation= "column";
// Dropdown list
var groupOne = mainWindow.add("group",undefined,"groupOne");
groupOne.orientation = "row";
groupOne.add("StaticText",undefined,"Main Comp");
var mainDD = groupOne.add("dropdownlist",undefined,compArrayNames);
mainDD.size = [175,25];
var groupTwo = mainWindow.add("group",undefined,"groupTwo");
groupTwo.orientation = "row";
groupTwo.add("StaticText",undefined,"Diff File");
var diffDD = groupTwo.add("dropdownlist",undefined,VideoArrayNames);
diffDD.size = [175,25];
// Button code
var createBT = mainWindow.add("group",undefined,"createBT");
createBT.orientation = "column";
var mainbutton = createBT.add("button",undefined,"Create");
mainWindow.center();
mainWindow.show();
// Main Code Begins
mainbutton.onClick = function(){
createCP();
}
// Main Function Code
function createCP() {
app.beginUndoGroup("Editing");
var comlayer = compArray[mainDD.selection.index];
var vidlayer = VideoArray[diffDD.selection.index];
var getcomp = app.project.items.addComp(VideoArrayNames[diffDD.selection.index]+"_DIFF".toString(),1920,1080,1,comlayer.duration,30);
getcomp.openInViewer();
addVideofile(comlayer,getcomp);
addvideotwofile(vidlayer);
renderQ();
app.endUndoGroup();
}
function addVideofile(comlayer) {
app.project.activeItem.layers.add(comlayer);
}
function addvideotwofile(vidlayer){
var newlayer = app.project.activeItem.layers.add(vidlayer);
newlayer.blendingMode = BlendingMode.DIFFERENCE;
// var outputModule = item.outputModule()
}
function renderQ(){
var Qcomp =app.project.activeItem;
var Qitem = app.project.renderQueue.items.add(Qcomp);
}
You have an invisible Zero-Width Joiner between
buildUI(thisObj); ‍
and
function buildUI(thisObj) {
and again after
//myPanel.show();
const toHex = str => {
var result = '';
for (var i=0; i<str.length; i++) {
result += str.charCodeAt(i).toString(16);
}
return result;
};
const stringWithJoiner = ` ‍`; // space (20) and joiner (200D)
console.log(toHex(stringWithJoiner))
Here is your script without it
(function(thisObj) {
buildUI(thisObj);
function buildUI(thisObj) {
var windowName = "Diffrence Checker";
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("window", windowName, undefined, {
resizeable: true
});
// Dropdown list
var groupOne = myPanel.add("group", undefined, "groupOne");
groupOne.orientation = "row";
groupOne.add("StaticText", undefined, "Main Comp");
var mainDD = groupOne.add("dropdownlist", undefined, compArrayNames);
mainDD.size = [175, 25];
var groupTwo = myPanel.add("group", undefined, "groupTwo");
groupTwo.orientation = "row";
groupTwo.add("StaticText", undefined, "Diff File");
var diffDD = groupTwo.add("dropdownlist", undefined, VideoArrayNames);
diffDD.size = [175, 25];
// Button code
var createBT = myPanel.add("group", undefined, "createBT");
createBT.orientation = "column";
var mainbutton = createBT.add("button", undefined, "Create");
//myPanel.center();
//myPanel.show();
myPanel.onResizing = myPanel.onResize = function() {
this.layout.resize();
};
if (myPanel instanceof Window) {
myPanel.center();
myPanel.show();
} else {
myPanel.layout.layout(true);
myPanel.layout.resize();
}
}
// Write your helper functions here
var compArray = [];
var compArrayNames = [];
var VideoArray = [];
var VideoArrayNames = [];
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i).hasVideo == true && app.project.item(i).file == null) {
compArray.push(app.project.item(i));
compArrayNames.push(app.project.item(i).name);
}
if (app.project.item(i).hasVideo == true && app.project.item(i).hasAudio == true) {
VideoArray.push(app.project.item(i));
VideoArrayNames.push(app.project.item(i).name);
}
}
function createCP() {
app.beginUndoGroup("Editing");
var comlayer = compArray[mainDD.selection.index];
var vidlayer = VideoArray[diffDD.selection.index];
var getcomp = app.project.items.addComp(VideoArrayNames[diffDD.selection.index] + "_DIFF".toString(), 1920, 1080, 1, comlayer.duration, 30);
getcomp.openInViewer();
addVideofile(comlayer, getcomp);
addvideotwofile(vidlayer);
renderQ();
app.endUndoGroup();
}
function addVideofile(comlayer) {
app.project.activeItem.layers.add(comlayer);
}
function addvideotwofile(vidlayer) {
var newlayer = app.project.activeItem.layers.add(vidlayer);
newlayer.blendingMode = BlendingMode.DIFFERENCE;
}
function renderQ() {
var Qcomp = app.project.activeItem;
var Qitem = app.project.renderQueue.items.add(Qcomp);
}
})
(this);

How to insert same item into nested array

How to insert same item into nested array. Here is my input element.
var myData1 = [{data1: 1},{data2:2}];
var myData2 = [{data1: 1},{data2:2}];
var myData3 = [{data1: 1},{data2:2}];
var key = [myData1, myData2, myData3];
var myVale = {
someValue :"SomeValue",
myData: [],
myVal: []
}
ANd this is my expected o/p
var myVale = {
someValue :"SomeValue",
myData: myData1,
myVal:[{someValue :"SomeValue",
myData: myData2,
myVal : [{
someValue :"SomeValue",
myData: myData3,
}]
}]
}
What I am trying here is
for(var i=0; i<key.length;i++){
myVale.myVal.push(myVale);
myVale.myData.push(key[i])
}
But here I am not getting an expected result. The output is going into loop. Any suggestion how I can get that?
I changed the input to ensure it is working as expected
var myData1 = [{data11: 1},{data12:2}];
var myData2 = [{data21: 1},{data22:2}];
var myData3 = [{data31: 1},{data32:2}];
var key = [myData1,myData2,myData3];
var myVale = {};
var index = 0;
function formatJSON(key, index) {
var tempVale = {};
tempVale.someValue = "SomeValue"+index;
tempVale.myData = key[index];
if(++index >= key.length)
return tempVale;
tempVale.myVal = formatJSON(key, index);
return tempVale;
}
myVale = formatJSON(key, index);
console.log(myVale);
console.log(JSON.stringify(myVale));

Lua table to js array

I have a lua table like
Save = {
["player 1"] = {
["show"] = true,
["name"] = "user1",
["data"] = 56171308,
},
["player 2"] = {
["show"] = false,
["name"] = "user1",
["data"] = 508703367,
},}
And i want to convert it to a js array through an html page to look like
[ ["player 1","user1",56171308],
["player 2","user2",508703367] ]
I have tried to load the lua content on page and remove some elements with this function
<script>
function myFunction() {
var str = document.getElementById("file-content").innerHTML;
var res = str.replace(/Save|show|true|false|name|data|,|{|}|=|"|[[\]]/g, '');
var temp = new String(res);
temp = temp.replace(/^\s*[\r\n]/gm, '');
document.getElementById("file-content").innerHTML = temp;
}
</script>
Here is updated answer for you issue:
function myFunction() {
var str = document.getElementById("file-content").innerHTML;
var res = str.replace(/Save|show|true|false|name|data|,|{|}|=|"|[[\]]/g, ' '); // extra white space replace
var temp = new String(res);
temp = temp.replace(/^\s*[\r\n]/gm, '');
temp = temp.replace(/(\r\n|\n|\r)/gm, "");
temp = temp.replace(/\t/g, '');
var array = temp.split(" ");
array = array.filter(function(e) {
return e
});
var finalArray = chunkArray(array, 3)
console.log(finalArray);
var longstring = convertToSring(finalArray);
document.getElementById("file-content").innerHTML = longstring.toString();
}
function chunkArray(myArray, chunk_size) {
var index = 0;
var arrayLength = myArray.length;
var tempArray = [];
for (index = 0; index < arrayLength; index += chunk_size) {
myChunk = myArray.slice(index, index + chunk_size);
// Do something if you want with the group
tempArray.push(myChunk);
}
return tempArray;
}
function convertToSring(finalArray) {
var longstring = "[";
for (var i = 0; i < finalArray.length; i++) {
var string = '["' + finalArray[i].join('","') + '"],';
console.log(string);
longstring = longstring + string;
}
longstring = longstring.replace(/,\s*$/, ""); // remove last comma
longstring = longstring + "]";
return longstring;
}
myFunction();
Check the jsfiddle working code : https://jsfiddle.net/gcfs0kda/2/

how to find index of array using substring value in javascript and replace with a new string?

I am getting string -
["1-2","10-4","2-3","3-1","4-4","5-2","6-4","7-3","8-1","9-2"]
as output from GetOption function where event - QuestionID & event1 -OptionID
Project-Online Examination System
var getValue;
var getName = new Array();
var temp = new Array();
function GetOption(event, event1) {
debugger;
if (temp.includes(event)) {
var x = getName.indexOf(event);
getName.splice(x - 1, 1);
getName.includes(event);
}
this.event1 = event1;
temp.push(event);
var getValue = event + "-" + event1;
if (getValue == "undefined-undefined") {
getName.push("");
} else {
getName.push(getValue);
getName.sort();
alert(getName);
}
$("#resultHidden").val(getName);
}
var items = ["1-2","10-4","2-3","3-1","4-4","5-2","6-4","7-3","8-1","9-2"];
var searchItems = "1-2";
var newItems = "2-1";
for (var i = 0; i < items.length; i++) {
if (items[i].startsWith(searchItems)) {
items[i] = newItems;
}
}

How could i get the result as trie format? object to string format from this code.

My expected output is ["Mark", "Mary", "Martod", "Mariam", "Marg"]
Getting output in console is ["Mark", "Mary", "Mart", "Mari", "Marg"]
How could I get the output as my expected result from this code? Can anyone do this for me?
function get_suggestion_array_from_object(searchstring, current_object) {
var suggestion_array = [];
suggestion_array.push(searchstring);
// console.log(suggestion_array);
append_object_key(suggestion_array, current_object);
}
var test_searchstring = 'Mar';
var test_current_object_string = '{"k":0,"y":0,"t":{"o":{"d":0}},"i":{"a":{"m":0}},"g":0}';
var test_current_object = JSON.parse(test_current_object_string);
console.log(test_current_object);
get_suggestion_array_from_object(test_searchstring, test_current_object);
function append_object_key(suggestion_array, current_object) {
var keys = Object.keys(current_object);
// CONCATENATE WITH SUGGESTION ARRAY ELEMENTS
var new_suggestion_array = [];
for (var i = 0; i < keys.length; i++) {
var current_key = keys[i];
var array_to_push = suggestion_array.slice();
for (var j = 0; j < suggestion_array.length; j++) {
array_to_push[j] = array_to_push[j] + current_key;
}
new_suggestion_array = new_suggestion_array.concat(array_to_push);
}
console.log(new_suggestion_array);
}
There are several approaches:
const get_suggestion_array_from_object = (searchstring, current_object_string) =>
current_object_string
.split(/[^a-z]+0[^a-z]+/)
.reduce((a, e) => e && a.concat(searchstring + e.replace(/[^a-z]+/g, '')) || a, [])
let searchstring = 'Mar';
let current_object_string = '{"k":0,"y":0,"t":{"o":{"d":0}},"i":{"a":{"m":0}},"g":0}';
console.log(get_suggestion_array_from_object(searchstring, current_object_string));
...
var test_searchstring = 'Mar';
var test_current_object_string = '{"k":0,"y":0,"t":{"o":{"d":0}},"i":{"a":{"m":0}},"g":0}';
var test_current_object = JSON.parse(test_current_object_string);
function get_suggestion_array_from_object(s, o) {
return Object.keys(o).map(function(k) {
var e = k;
var no = o[k];
while (no) {
var nk = Object.keys(no)[0];
e += nk;
no = no[nk];
}
return s + e;
})
}
console.log(get_suggestion_array_from_object(test_searchstring, test_current_object))

Categories

Resources