Syntax error js object to json [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hello i'm using Backbone here i have an issue, my 'history' attribute will be
history = history + ' * ' + 'Attribute "used" modified from ' + !this.changed.used + ' to ' + this.changed.used + ' by ' + LOGGED_USER.get("fisrtName") + " " + LOGGED_USER.get("name") + ' the ' + (new Date());
But when i do
this.set("history", history);
var json = JSON.stringify(this.toJSON())
the json returned is kind of corrupted with \
Any idea ?

Because the " needs to be escaped in strings, it is not corrupted.
{ "key" : "I am \"valid\"" }

Related

Need help to understand Vue.js function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an assignment to make calculator using vue.js.
Much of it works, I'm confused about how to implement the '=' function.
I found this piece of code in an article that does something similar but I don't understand what it does. Can someone explain it?
https://scotch.io/tutorials/build-a-scientific-calculator-with-vuejs
I found this Piece of code:
if ((app.current).indexOf("^") > -1) {
var base = (app.current).slice(0, (app.current).indexOf("^"));
var exponent = (app.current).slice((app.current).indexOf("^") + 1);
app.current = eval("Math.pow(" + base + "," + exponent + ")");
} else {
app.current =app.current
}
Can someone please explain what the above function does, line-by-line?
// checks if app.current have '^' by getting the index
// if the method indexOf doesn't find anything it return -1
if (app.current.indexOf('^') > -1) {
// gets the first element till the index where it found '^'
var base = app.current.slice(0, app.current.indexOf('^'));
// gets the number after the 'ˆ'
var exponent = app.current.slice(app.current.indexOf('^') + 1);
// eval is evil
// it gets the string and transfoms into valid code
// wich means to do the operation
app.current = eval('Math.pow(' + base + ',' + exponent + ')');
} else {
// if it doesn't find the index it keeps the same value
app.current = app.current;
}
https://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/

I am missing something [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I've looked this code over and over.. I typed it as I was learning from lesson material, and have checked and checked over and over again. the code works for the instructor, but not me. Is it my mistake or is it a problem for the platform I am using? It not working may seems trivial comparing to needing to understand what is being taught, but I still would like to understand all that I can. thank you. https://codepen.io/Slimmwillis/pen/EQWpGP?editors=1111
function yearsUntilRetirement(name, year) {
var age = calculateAge(year);
var retirement = 65 - age;
if (retirement >= 0) {
console.log(name + ' retires in ' + retirement + ' years.');
} else {
console.log(name + ' is already retired.');
}
}
yearsUntilRetirment('John', 1990);
you need to be very careful while typing. You're not calling the right function so nothing happens.
function yearsUntilRetirement(name, year) {
var age = calculateAge(year);
console.log('hello');
var retirement = 65 - age;
if (retirement >= 0) {
console.log(name + " retires in " + retirement + " years.");
} else {
console.log(name + " is already retired.");
}
}
yearsUntilRetirment("John", 1990);
Check the yearsUntilRetirment("John", 1990); ! the name doesn't match the function.

'Undefined' JSON data [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
This is my first time using JSON. I have an external json file and I am using the json file in my html file to create divs with every json data read. I can get the correct output for all of the values, except for one which is the 'descript' value. I have tried printing it to the cosole log but all I'm getting for that particular value is 'undefined' but the other values are alright. Any idea on why this happened?
JSON file:
[{"title":"3G","filePath":"https://example.com","descript":"hello world"}, {"title":"4G", "filePath":"https://example.com", "descript": "test"} ]
HTML file:
$.ajax({
url : "testJSON.json",
type : "get", // whichever you like
contentType:"json",
success : function(list)
{
var divCol = "<div class='col-sm-4 col-md-4'>";
var divWell = "<div class='well'>";
var divClose= "</div>";
list.forEach(function(obj, index) {
var title = "<h5>" + obj.title + "</h5>";
var desc = "<p>" + obj.descript + "</p>";
var linkStart = "<a class='btn btn-default' style='float:left' href='" + obj.filePath + "' target='_blank'>";
var linkEnd = "CSV</a>";
var div = divCol +
divWell +
title +
desc +
linkStart +
// image +
linkEnd +
divClose +
divClose;
console.log(list)
$("#imdaFiles").append(div); // insert the div you've just created
})
}
});
Just a short notice that might solve the issue.. you misplaced a double quote on the right side of the last descript key in your JSON.

A regex phone reformatting for JS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have phone number like: 89227611508 and i need to reformat this with regex(JS) into +8 922 761-15-08. I'm new in regex and can't get it done. Can anyone provide a simple solution?
Use:
"89227611508".replace(/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/, "+$1 $2 $3-$4-$5");
You need to use a regular expression to grab the groups of numbers then concatenate the number groups into a formatted string.
let number = '89227611508';
console.log(formatNumber(number));
function formatNumber(number) {
let groups = number.match(/^(\d)(\d{3})(\d{3})(\d{2})(\d{2})$/);
return '+' + groups[1] + ' ' + groups[2] + ' ' + groups[3] + '-' + groups[4] + '-' + groups[5];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

Combine to get new variable name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to get this: $('#anchor')
Where anc hor are variables themselves.
var firstPart = "anc"
var secondPart = "hor"
How do I do that? + to the question (sorry for that) if I have this structure:
<a id="233"></a><li>qwerty</li>
how do I addClass to this exact li that comes after #233?
First part:
$('#' + firstPart + secondPart); // $('#anchor')
Second part:
$('#123 + li').addClass('myclass');
http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
Answer1
var firstPart = "anc"
var secondPart = "hor"
var selector = "#" + firstPart + secondPart;
Then you can call it by $(selector) .
Answer 2:
$(function(){
$('#233 + li').addClass('ownclass');
});
Check http://jsfiddle.net/alaminopu/U2q6E/ for Answer 2.
'#anchor' is just a string. You use standard string concatination.
var str = '#' + firstPart + secondPart;
$(str)

Categories

Resources