Lua table to js array - javascript

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/

Related

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 to separate a string by words using arrays and function?

Code the I am trying to do:
function look(str) {
var stringArr = ['JAVA'];
var arr = [];
var novaString = ''
for(i = 0; i < stringArr.length; i++) {
arr = stringArr;
}
console.log(arr)
return arr;
} look('arr');
I Want the output to look like this:
[J]
[JA]
[JAV]
[JAVA]
[JAV]
[JA]
[J]
There's no more details to add.
This will do
var k='JAVA';
var a=k.split('');
var e=[];
a.forEach((el)=>{
e.push(el);
console.log(e);
})
for(let i=0;i<a.length-1;i++)
{
e.pop();
console.log(e);
}
Your question is not so clear but may be you want this:
function look(str) {
var stringArr = str;
for(i = 0; i < stringArr.length; i++)
{
console.log("[" + stringArr.substring(0,i+1) + "]");
}
for(i = (stringArr.length - 2); i >= 0; i--)
{
console.log("[" + stringArr.substring(0,i+1) + "]");
}
}
look('arr');

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

make a link in angular custom filter

I have make a custom filter in angular to first extract this pattern #[1:abc] from a string into into abc.Here is my code
Html :
<div class="mts mbs lg-feed-content-txt" hm-read-more hm-limit="200" hm-more-text="more" hm-less-text="less" hm-dots-class="toggle-dots-grey" hm-text="{{activity.post.text | mentionParser:activity.post}}" hm-link-class="toggle-readmore"></div>
Javasctipt:
.filter('mentionParser', function() {
return function(text, res) {
var text;
var regex = /[#[0-9]+[:]([a-z|A-Z ]+)]/g;
var idSeprator = /[0-9]+/g;
var displaySeprator = /([a-z|A-Z ]+)/g;
var pattern = text.match(regex);
var uidList = [];
var textCopy = text;
var mentions = [];
if (pattern != null) {
for (var j = 0; j < pattern.length; j++) {
var name ='<a>'+ res.mentionList[j].name +'</a>';
textCopy = textCopy.replace(pattern[j], name );
}
return textCopy;
} else {
return text;
}
}
});
What i want is that to make abc name linkable

javascript, keep getting undefined in loop

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)

Categories

Resources