An alternative method for JSON.stringify() - javascript

I want an alternative method for JSON.stringify().
I am using JSON.stringify and I got error like cyclic object value. I don't know how to remove this error so I'd like to know if there's an alternative method.
DCSSPACE.SaveAndOpenJSONWriter = function(canvas) {
//multiple tab.....
var mydata = [];
var area = {}, DCS = {}, workspace = {};
var len = DCSSPACE.collection.length;
for (var k = 0; k < len; k++) {
var result = [];
var tabid = DCSSPACE.collection.models[k].id;
var canvas = DCSSPACE.collection.get(tabid).get("workflow");
//for node
var figures = canvas.getFigures();
for (var i = 0; i < figures.getSize(); i++) {
if (figures.get(i).type == "draw2d.Node") {
var node = {};
node.blockType = getBlockType(figures.get(i));
node.x = figures.get(i).getX();
node.y = figures.get(i).getY();
node.id = figures.get(i).getId();
node.type = figures.get(i).type;
node.width = figures.get(i).getWidth();
node.height = figures.get(i).getHeight();
node.label = figures.get(i).getLabel();
node.sequenceNo = figures.get(i).getSequenceNo();
node.model = figures.get(i).model;
result.push(node);
}
}
//for lines
var lines = canvas.getLines();
for (var j = 0; j < lines.getSize(); j++) {
if (lines.get(j).type == "draw2d.nodeConnetion") {
var nodeConnection = lines.get(j).getConnectionAttrubutes();
nodeConnection.source = lines.get(j).getSource();
nodeConnection.target = lines.get(j).getTarget();
result.push(nodeConnection);
}
}
area = {
tabid : tabid,
attr : result
};
mydata.push(area);
result=[];
workspace = {
DCS : mydata
};
}
//console.log(mydata);
var arr = JSON.stringify(workspace, null, 4);
console.log(arr);
DCSSPACE.SaveAndLoadFigure = result;
return workspace;
};

If won't to use stringify you can reproduce it, like this:
OBJtoString(object [,n]) accempt 2 args:
object (needed) the object you need to log (in my release array and object is the same)
n numbero of space that indent every new line inside object aruments.
var OBJtoString = function(_o,_m,_rf,_dep,_res){
_dep = [],
_rf = function(obj,n,_n,_a,_k,_i,_ws,_f,_e){
if(typeof obj != "object") return false;
if(typeof _a === "undefined") _a = "FIRST PARENT OBJECT";
_dep.push(obj),
_f = (_dep.length < 1) ? function(){} : function(_z,_x){
for(_x = 0; _x <= _dep.length; _x++){
if(obj[_z] == _dep[_x]) return true;
}
return false;
}
_ws = "";
if(typeof n === "undefined") n = 1;
if(typeof _n === "undefined") _n = n;
for(_k = 0; _k <= n; _k++){
_ws += " ";
}
var response ="{ \n";
for(var _i in obj){
if(typeof obj[_i] !== "object"){
if(typeof obj[_i] === "string") obj[_i] = "'"+obj[_i].toString()+"'";
if(typeof obj[_i] === "boolean") obj[_i] = obj[_i].toString() + " (boolean)";
response += _ws + _i + " : " + obj[_i].toString();
response += "\n";
continue;
}
response += (_f(_i)) ? _ws + _i + " : "+ _a +" (prevent loop)\n" : _ws + _i + " : " + _rf(obj[_i],n+_n,_n,_a);
}
if(_n != n) response += _ws;
return response +="} \n";
}
_res = _rf(_o,_m);
_dep = [];
return _res;
}
Uses Example:
var example = {ciao : "hellow", saluto : {ciao : "ciao", dam : true}};
example.parentObj = example;
console.log(OBJtoString(example,4));
return:
{
ciao : 'hellow'
saluto : {
ciao : 'ciao'
dam : true (boolean)
}
parentObj : FIRST PARENT OBJECT (prevent loop)
}
Other Example:
var example = {ciao : "hellow", saluto : {ciao : "ciao", dam : true}};
example.parentObj = example;
example.f = {
g : example
}
console.log(OBJtoString(example,4));
Return:
{
ciao : 'hellow'
saluto : {
ciao : 'ciao'
dam : true (boolean)
}
parentObj : FIRST PARENT OBJECT (prevent loop)
f : {
g : FIRST PARENT OBJECT (prevent loop)
}
}

I found util.inspect() pretty helpful.
You can print entire object
console.log(util.inspect(myObj))
Or with optional arguments
util.inspect(object, showHidden=false, depth=2, colorize=true)
Documentation:
https://nodejs.org/en/knowledge/getting-started/how-to-use-util-inspect/

Related

isset equivalent in javascript to find palindrome

I created a script in PHP to find a palindrome, but when I try to do the same in JavaScript, then it is not working as expected. It's not just a matter of checking if the string that is reversed matches, but any order of the string has to be checked as well.
In other words, "mom" should return as true, "mmo" should return as true, "omm" should return as true, etc..., which is what the PHP script does, but the JS script below doesn't even work for the first iteration for the string "mom"
The following is the PHP script:
<?php
function is_palindrom($str) {
$str_array = str_split($str);
$count = array();
foreach ($str_array as $key) {
if(isset($count[$key])) {
$count[$key]++;
} else {
$count[$key] = 1;
}
}
$odd_counter = 0;
foreach ($count as $key => $val) {
if(($val % 2) == 1) {
$odd_counter++;
}
}
return $odd_counter <= 1;
}
echo is_palindrom('mom') ? "true" : "false";
The following is what I have tried in JS:
var count = [];
var strArr = [];
var oddCounter = 0;
var foreach_1 = function(item, index) {
console.log("count[index]: " + count[index]);
if (typeof count[index] !== "undefined") {
count[index]++;
} else {
count[index] = 1;
}
};
var foreach_2 = function(item, index) {
console.log("item: " + item + " item % 2: " + eval(item % 2));
if (eval(item % 2) == 1) {
oddCounter++;
}
console.log("oddCounter: " + oddCounter);
return oddCounter <= 1;
};
var isPalindrom = function(str) {
strArr = str.split("");
console.log(strArr);
strArr.forEach(foreach_1);
console.log(count);
count.forEach(foreach_2);
};
I believe it is failing where I try to replicate isset in javascript, with the following code:
if (typeof count[index] !== "undefined") {
As a result, I have tried to write my own isset function, but still the same result, it is not working:
var isset = function(obj) {
if (typeof obj === "undefined" || obj === null) {
return false;
} else {
return true;
}
};
With the following function being called:
if (isset(count[index])) {
count[index]++;
} else {
count[index] = 1;
}
As usual, any help would be appreciated and thanks in advance
BTW, it's killing me that I cannot remember the word for several revisions or iterations of something - I know that it starts with "re"
My attempt:
let p1 = `No 'x' in Nixon.`
let p2 = `Was it a car or a cat I saw?`
let p3 = `A man, a plan, a canal, Panama!`
function is_palindrome (str) {
const normalize = str => str.replace(/[.,:;`'"!?\/#$%\^&\*{}=\-_~()\s]/g, '').toLowerCase()
const reverse = str => [...str].reverse().join('')
return normalize(str) === reverse(normalize(str))
? true
: false
}
console.log(is_palindrome(p1))
console.log(is_palindrome(p2))
console.log(is_palindrome(p3))
First, thank you for all the comments.
Second, I ran a var_dump on the count array in the PHP file and this was the result:
array (size=2)
'm' => int 2
'o' => int 1
Which lead me to understand that count in js has to be an object for this work and I would have to create indexes of the object, depending on the string entered.
One thing lead to another and a complete re-write, but it works, along with a spell checker - see link at the bottom for complete code:
var count = {};
var strArr = [];
var oddCounter = 0;
var objKeys = [];
var splitString;
var reverseArray;
var joinArray;
var url = "test-spelling.php";
var someRes = "";
var mForN = function(obj, strArr) {
for (var y = 0; y < strArr.length; y++) {
// console.log("obj[strArr[" + y + "]]: " + obj[strArr[y]]);
if (isset(obj[strArr[y]])) {
obj[strArr[y]]++;
} else {
obj[strArr[y]] = 1;
}
}
return obj;
};
var mForN_2 = function(obj, objKeys) {
for (var z = 0; z < objKeys.length; z++) {
/* console.log(
"obj[objKeys[z]]: " +
obj[objKeys[z]] +
" obj[objKeys[z]] % 2: " +
eval(obj[objKeys[z]] % 2)
); */
if (eval(obj[objKeys[z]] % 2) == 1) {
oddCounter++;
}
// console.log("oddCounter: " + oddCounter);
}
return oddCounter <= 1;
};
var isset = function(obj) {
if (typeof obj === "undefined" || obj === null) {
return false;
} else {
return true;
}
};
var isPalindrom = function(str) {
// reverse original string
splitString = str.split("");
reverseArray = splitString.reverse();
joinArray = reverseArray.join("");
var checking = checkSpellingOfStr(str);
if (str == joinArray) {
strArr = str.split("");
// console.log("strArr: " + strArr);
objKeys = makeObjKeys(count, strArr);
// console.log("filled count before mForN: " + JSON.stringify(count));
// create array of keys in the count object
objKeys = Object.keys(count);
// console.log("objKeys: " + objKeys);
count = mForN(count, strArr);
// console.log("count after mForN: " + JSON.stringify(count));
return mForN_2(count, objKeys);
} else {
return 0;
}
};
var makeObjKeys = function(obj, arr) {
for (var x = 0; x < arr.length; x++) {
obj[arr[x]] = null;
}
return obj;
};
var checkSpellingOfStr = function(someStr) {
var formData = {
someWord: someStr
};
$.ajax({
type: "GET",
url: url,
data: formData,
success: function(result) {
if (!$.trim(result)) {
} else {
console.log(result);
$("#checkSpelling").html(result);
}
}
});
};
Start everything with the following call:
isPalindrom("mom") ? demoP.innerHTML = "is pal" : demoP.innerHTML = "is not pal";
In my example, I have a form and I listen for a button click as follows:
var palindromeTxt = document.getElementById("palindromeTxt").value;
var btn = document.getElementById("button");
btn.addEventListener("click", function (event) {
isPalindrom(palindromeTxt) ? demoP.innerHTML = "is pal" : demoP.innerHTML = "is not pal";
});
The following is the php for spell check:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(!empty($_REQUEST['someWord']))
{
$someWord = $_REQUEST['someWord'];
}
$pspell_link = pspell_new("en");
if (pspell_check($pspell_link, $someWord)) {
echo trim($someWord) . " is a recognized word in the English language";
} else {
echo "Your word is either misspelled or that is not a recognized word";
}
You will need pspell installed on your server, as well as adding extension=pspell.so to your php.ini
This is what I did, to get it running locally on my mac:
cd /Users/username/Downloads/php-5.6.2/ext/pspell
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/php5-5.6.2-20141102-094039/bin/php-config --with-pspell=/opt/local/
make
cp ./modules/* /usr/local/php5-5.6.2-20141102-094039/lib/php/extensions/no-debug-non-zts-20131226
sudo apachectl restart
check your phpinfo file and you should see the following:
pspell
PSpell Support enabled
Live example

Create Array of Objects and count number of occurrence

I have an array of objects and want to create another array of objects based on.
I want to check if an object is repeated just want to show the count, otherwise show the object itself with count = 1.
<!-- I have an array-->
var arr =[{name:"coke",price:20},{name:"coke",price:20},{name:"coke",price:20},{name:"kabab",price:250}];
// I want to create another array based on "arr" like the one below
var test =[{name:"coke",price:20,count:3},{name:"kabab",price:20,count:1}];
//Any hint please
This may help you. This answer considers name or some identifier will be unique for each object.
counter = {}
var arr = [{
name: "coke",
price: 20
}, {
name: "coke",
price: 20
}, {
name: "coke",
price: 20
}, {
name: "kabab",
price: 250
}];
var obj = {};
var counter = {}
for (var i = 0, len = arr.length; i < len; i++) {
obj[arr[i]['name']] = arr[i];
counter[arr[i]['name']] = (counter[arr[i]['name']] || 0) + 1
}
newArr = new Array();
for (var key in obj){
newArr.push(extend( obj[key], {count:counter[key]}));
}
function extend(a, b){
for(var key in b)
if(b.hasOwnProperty(key))
a[key] = b[key];
return a;
}
console.log(newArr)
var arr =[{name:"coke",price:20},{name:"coke",price:20},{name:"coke",price:20},{name:"kabab",price:250}];
var countNameMapping = {}, finalArr = [];
var arrLength = arr.length;
for(i=0; i<arrLength; i++){
var tempObj = {name:arr[i], price:arr[i].price, occurance:1};
var productName = arr[i].name;
if(countNameMapping[productName] === undefined){
countNameMapping[productName] = tempObj;
}else{
countNameMapping[productName].occurance += 1;
}
}
for(var k in countNameMapping){
finalArr.push(countNameMapping[k])
}
console.log(finalArr );
You can try this one:
var arr =[{name:"coke",price:20},{name:"coke",price:20},{name:"coke",price:20},{name:"kabab",price:250}];
var result = [];
arr.map(function(arrObject) {
if (result.length > 0) {
result.map(function(resultObject) {
if (resultObject.name != arrObject.name) {
arrObject.count = 1;
result.push(arrObject);
} else {
resultObject.count++;
}
})
} else {
arrObject.count = 1;
result.push(arrObject);
}
})
console.log(result);
This will provide the result you are looking for:
var arr =[{name:"coke",price:20},{name:"coke",price:20},{name:"coke",price:20},{name:"kabab",price:250}];
var map = arr.reduce((accum, item) => {
var obj = accum.get(item.name) || Object.assign({}, item, {count:0});
obj.count++;
return accum.set(item.name, obj);
}, new Map());
var res = [...map.values()];
More or less...
var arr = [{
name: "coke",
price: 20
}, {
name: "coke",
price: 20
}, {
name: "coke",
price: 20
}, {
name: "kabab",
price: 250
}];
// I want to create another array based on "arr" like the one below
// var test =[{name:"coke",price:20,count:3},{name:"kabab",price:20,count:1}];
var count = {};
var test = [];
for (var i = 0, len = arr.length; i < len; i++) {
var id = JSON.stringify(arr[i]);
if (count.hasOwnProperty(id)) {
count[id].count++;
} else {
test.push(arr[i]); // Data contamination. Too lazy to copy object
count[id] = test[test.length - 1]; // Could be better.
count[id].count = 1;
}
}
console.log(test);
This is probably what are you looking for:
How does it work?
First, your array arr will use a forEach loop to find each object and if if new you will add it to the results array. The method isNew() will return true if the object is new.
For each new object founded you will count the number of occurrences using findOccurrences() To reduce the number of "loops" you will slice the array according to the index. So you don't need to search again over the already processed data.
So now you can build an new object, using the name, price and count.
Finally, you can push() the new object to the results array.
var arr =[{name:"coke",price:20},{price:20,name:"coke"},{name:"coke",price:20},{name:"kabab",price:250}];
var results = [];
var index = 0;
var originalDiv = document.getElementById('original');
var resultsDiv = document.getElementById('results');
arr.forEach(function(obj) {
if (isNew(obj)) {
var counter = findOccurrences(obj, arr.slice(index, arr.length));
var newObj = {
name: obj.name,
price: obj.price,
count: counter
}
results.push(newObj);
}
index++;
});
printArray(arr, originalDiv);
printArray(results, resultsDiv);
function isNew(newObj) {
var wasFound = true;
if (typeof results != "undefined" && results != null && results.length > 0) {
results.forEach(function(obj) {
if (newObj.name === obj.name && newObj.price === obj.price) {
return false;
} else {
wasFound = false;
}
});
return !wasFound;
} else {
return true;
}
}
function findOccurrences(newObj, objects) {
var count = 0;
if (typeof objects != "undefined" && objects != null && objects.length > 0) {
objects.forEach(function(obj) {
if (newObj.name === obj.name && newObj.price === obj.price) {
count++;
}
});
}
return count;
}
function printArray(objects, div) {
var count = 0;
if (typeof objects != "undefined" && objects != null && objects.length > 0) {
objects.forEach(function(obj) {
var newElement = document.createElement('p');
newElement.innerHTML = 'item ' + count + ': ';
Object.keys(obj).forEach(function(key) {
newElement.innerHTML += key + ': ' + obj[key] + ', ';
});
newElement.innerHTML = newElement.innerHTML.slice(0, -2);
div.appendChild(newElement);
count++;
});
}
}
<div id="original"><p>Original Array</p></div>
<div id="results"><p>Results Array</p></div>
Update:
More optimization.
var arr =[{name:"coke",price:20},{name:"coke",price:20},{name:"coke",price:20},{name:"kabab",price:250},{name:"coke",price:20},{name:"coke",price:20},{name:"kabab",price:250}];
var accumulator = {};
var results = [];
var index = 0;
var originalDiv = document.getElementById('original');
var resultsDiv = document.getElementById('results');
String.prototype.hashCode = function() {
var hash = 0;
if (this.length == 0) return hash;
for (i = 0; i < this.length; i++) {
var char = this.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash |= 0; // Convert to 32bit integer
}
var c = (hash & 0x0FFFFFFF)
.toString(16)
.toUpperCase();
return '0000000'.substring(0, 7 - c.length) + c;
};
arr.forEach(function(obj) {
var id = JSON.stringify(obj).hashCode();
console.log(id);
if (accumulator.hasOwnProperty(id)) {
accumulator[id].count++;
} else {
results.push(obj);
accumulator[id] = results[results.length - 1];
accumulator[id].count = 1;
}
});
printArray(arr, originalDiv);
printArray(results, resultsDiv);
function printArray(objects, div) {
var count = 0;
if (typeof objects != "undefined" && objects != null && objects.length > 0) {
objects.forEach(function(obj) {
var newElement = document.createElement('p');
newElement.innerHTML = 'item ' + count + ': ';
Object.keys(obj).forEach(function(key) {
newElement.innerHTML += key + ': ' + obj[key] + ', ';
});
newElement.innerHTML = newElement.innerHTML.slice(0, -2);
div.appendChild(newElement);
count++;
});
}
}
<div id="original">
<p>Original Array</p>
</div>
<div id="results">
<p>Results Array</p>
</div>

Conver array of sting to array or JSON based on text

In my project I am stuck in this task.I have array of string like this:
var tempArray = [];
tempArray[0]="key1 : value1";
tempArray[1]="key1 : value2";
tempArray[3]="key2 : value1";
tempArray[4]="key2 : value2";
tempArray[5]="key2 : value3";
Now I want result something like this:
"key1":"value1","value2"
"key2":"value1","value2","value3"
You can use JSON or array I just want result in this formate.
You can use Array.prototype.reduce() to transform your array however you like:
var output = tempArray.reduce(function(result, item) {
var key = item.split(":")[0].trim();
var value = item.split(":")[1].trim();
if (result[key]) {
result[key].push(value);
} else {
result[key] = [value];
}
return result;
}, {});
You can use reduce here to good effect.
var out = tempArray.reduce(function (p, c) {
var arr = c.split(' : '), key = arr[0], value = arr[1];
p[key] = p[key] || [];
p[key].push(value);
return p;
}, {});
Note that this gives you an object the value of which are arrays. You can access them like this:
out.key1[0] // value1
DEMO
Try this function... I don't know if there is a specific function for this but I make a function which will do what you want...
function getMultArray(temp) {
var newArray = {};
var key;
var oldValue = null;
var tempA = [];
var i = 0;
var j = 0;
$.each(temp, function(k, value) {
var newValue = value.split(" : ");
j++;
if (oldValue === newValue[0]) {
tempA.push(newValue[1]);
if (temp.length == j) {
newArray[oldValue] = tempA;
tempA = [];
}
} else {
if (i === 0) {
i = 1;
oldValue = newValue[0];
tempA.push(newValue[1]);
if (temp.length == 1) {
newArray[oldValue] = tempA;
tempA = [];
}
} else {
newArray[oldValue] = tempA;
tempA = [];
tempA.push(newValue[1]);
oldValue = newValue[0];
if (temp.length == j) {
newArray[oldValue] = tempA;
tempA = [];
}
}
}
});
return newArray;
}
var tempArray = [];
tempArray[0] = "key1 : value1";
tempArray[1] = "key1 : value2";
tempArray[2] = "key2 : value1";
tempArray[3] = "key2 : value2";
tempArray[4] = "key2 : value3";
var newArray = getMultArray(tempArray);
$.each(newArray, function(key, value) {
$.each(value, function(i, value) {
$("#result").html($("#result").html()+"<br/>"+key + "-" + value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='result'></div>
try this
(function() {
var tempArray = [];
tempArray[0] = "key1 : value1";
tempArray[1] = "key1 : value2";
tempArray[3] = "key2 : value1";
tempArray[4] = "key2 : value2";
tempArray[5] = "key2 : value3";
function parse() {
var _tempArr = {}
tempArray.forEach(function(item) {
var k = item.replace(/\s+/, "").split(":");
if (_tempArr[k[0]] !== undefined) {
_tempArr[k[0]].push(k[1]);
} else {
_tempArr[k[0]] = [];
_tempArr[k[0]].push(k[1]);
}
});
console.log(_tempArr);
}
parse();
})()
I suggest to change the data formato to a more concise style with just only the key like 'key1' and the value like 'value1' and not a combined string like "key1 : value1".
The result is an object with the keys as properties and the values as elements in an array.
var object = {};
function addValueWithKey(key, value) {
object[key] = object[key] || [];
object[key].push(value);
}
addValueWithKey('key1', 'value1');
addValueWithKey('key1', 'value2');
addValueWithKey('key2', 'value1');
addValueWithKey('key2', 'value2');
addValueWithKey('key2', 'value3');
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

Can I use the 'in' keyword to test a property in an (tree) object

Let's say I've got the following object:
Variables.settings.backend.url = 'http://localhost';
What I would do to test is url is available, is do to the following:
if ('settings' in Variables && 'backend' in Variables.settings && 'url' in Variables.settings.backend) {
// true
}
This is quite cumbersome.
If this was PHP, i could just do
if (!empty($variables['settings']['backend']['url']) {
//true
}
Can this be done any simpler in JS?
I wrote a function to test this :
var isModuleDefined = function(moduleName) {
var d = moduleName.split(".");
var md = d[0];
for (var i = 1, len = d.length; i <= len; i++) {
if (eval("typeof " + md) !== "undefined") {
md = md + "." + d[i];
continue;
}
break;
}
return i === len + 1;
};
isModuleDefined("Variables.settings.backend.url");
but i really don't know about the cost-efficiency of that method, using eval.
Edit (Without eval..):
var isModuleDefined = function(moduleName) {
var d = moduleName.split(".");
var base = window;
for (var i = 0, len = d.length; i < len; i++) {
if (typeof base[d[i]] != "undefined") {
base = base[d[i]];
continue;
}
break;
}
return i === len;
};
Yet another variant
function isFieldExist(expression){
var fields = expression.split('.'),
cur = this;
for(var i=0; i<fields.length; i++){
if(typeof cur[fields[i]] === "undefined") return false;
cur = cur[fields[i]];
}
return true;
}
for using
isFieldExist("Variables.settings.backend.url"); // find in global scope
isFieldExist.call(Variables, "settings.backend.url"); // find in Variables

javascript check if array key is set

var place = []
place[0] = "Sant Marti de Canals";
place[1] = "Catalonia";
place[3] = "";
place[4] = "Spain";
placeTitle = place.join(",");
current output is "Sant Marti de Canals,Catalonia,,,Spain"
how can it be "Sant Marti de Canals,Catalonia,Spain"
You can also define a filter function, which is useful in many other situations.
Array.prototype.filter = function(p) {
var a = [];
p = p || function (x) {return x;}
for (var i = 0; i<this.length; ++i) {
if (p(this[i])) {
a.push(this[i]);
}
}
return a;
}
...
placeTitle = place.filter().join(', ');
Writing your own code for this can help
var place = []
place[0] = "Sant Marti de Canals"; place[1] = "Catalonia"; place[3] = ""; place[4] = "Spain";
var placeTitle ='';
for(var test in place) {
placeTitle += place[test]!=''?place[test]+',':'';
}
placeTitle = placeTitle.substr(0,placeTitle.length-1)
placeCopy=[];
for(var i=0;i<place.length;i++){
if(typeof place[i] != 'undefined' && place[i].length > 0){
placeCopy.push(place[i]);
}
}
placeTitle = placeCopy.join(",");

Categories

Resources