Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a string like this:
data="{'year':'1990/01/01','income':1990/02/01,'expenses':1000668},{'year':'1990/03/01','income':1000778,'expenses':1000778}"
I want to set it on one array like this:
var chart_data = [data];
how can I do this?
Assuming that first income value is meant to be either quoted or a numeric value, try this out
var chart_data = JSON.parse('[' + data.replace(/'/g, '"') + ']')
I had to convert all the single-quotes to double in order to make the string valid for JSON parsing.
Your data is almost JSON so you can manipulate it to match that format and then use JSON.parse() to convert it to a Javascript object.
The first issue is you need to convert ' to " because JSON uses " to wrap strings. You can use String.replace() to do this.
You also have several objects in the one string, but JSON can only give you one result. You can wrap the string in [] to get JSON.parse() to give you an array containing your objects.
The last thing I saw was the income for the first item had some loose /s which are not allowed in JSON. You'll need to wrap the income in " or remove the /s.
Here's an example:
var data = "{'year':'1990/01/01','income':19900201,'expenses':1000668},{'year':'1990/03/01','income':1000778,'expenses':1000778}";
data = '[' + data.replace(/'/g, '"') + ']';
var chart_data = JSON.parse(data);
Related
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 1 year ago.
Improve this question
I'm asked to convert ["abc","efg"]
output should be "["abc","efg"]"
How can I do it using javascript?
I have tried searching so much, but couldn't find anything.
Any help will be appreciated.
You can stringify the value using JSON.stringify():
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
var valArr = ["abc","efg"];
var valStr = JSON.stringify(valArr);
console.log(valStr);
console.log('The type of valStr is:', typeof(valStr))
console.log(JSON.stringify(["abc","efg"]))
Note this will look like "[\"abc\",\"efg\"]" when expressed as a double-quoted string since the inner quotes must be escaped. The value is ["abc","efg"] when printed, which is a string.
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 4 years ago.
Improve this question
I have a string like
Hi,
My
Name
is
Mike
How can I turn this into :
1.Hi,
2.My
3.Name
4.is
5.Mike
var numbered = `Hi,
My
Name
is
Mike`.split('\n').map((line, index) => `${index + 1}. ${line}`).join('\n')
console.log(numbered)
Breaking down the solution;
We take the original string and then split by the line-break character, so we get an array of strings (one per line)
Map is a function that allows us to apply a transformation function to every item of the array, returning a new one with the new items.
Map passes the current array item plus a zero based index. We concatenate the index with the current item so we get the string we expect
Since we still have an array (but we need a string) we use join method. Join method joins all items in the array with a given character, in this case we pass the line break character again so we have one line per item.
You can use String.replace() with a RegExp that matches the lines' start (regex101 example), and replace each with the line's number.
Note: regex suggested by Thomas in his comment.
const str = `Hi,
My
Name
is
Mike`;
let i = 1;
const numbered = str.replace(/^/gm, () => `${i++}.`);
console.log(numbered)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Let's say I have a string that looks like this:
Red#Yellow#Blue#Green
How can I use Javascript to remove the last instance of # as well as the text that comes after it, so that the resulting string would look like this:
Red#Yellow#Blue
string=string.split("#");
alert(string.pop());//Green
string=string.join("#");
I dont see a problem? Simply split by #, remove the last one and join again?
you can split the string into arrays and join all the items of the array except the last one
var myString = Red#Yellow#Blue#Green;
var myArrray = myString.split('#');
myArray.splice(myArray.length-1,1);
myArray.join('#');
console.log('Red#Yellow#Blue#Green'.replace(/\#[a-zA-Z]+$/,''));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the Text box .That text Box ID is text_1__val.
I need 1 from that Id.How to Get the Particular part of the textbox ID in jquery which means i need the between _ and __ from that textbox ID?
If the only requirement is that the character / characters appear between a single and double underscore, try this regular expression match
var rx = /_(.+?)__/;
var part = rx.test(idValue) && rx.exec(idValue)[1];
This assumes that you're only after the first of any occurrences in your ID value string. If the string fails to match, part will be false.
The split() method is used to split a string into an array of substrings, and returns the new array.
$(your_textbox).attr("id").split("_")[1]
//Syntax
string.split(separator,limit)
Function Reference: http://www.w3schools.com/jsref/jsref_split.asp
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
First of all I am new to javascript.
My question:
let's say I have a string like this
var str = "John Doe in the name used for people with no identity";
I need a function with 3 arguments something like
pullSubstring(text, string, number)
It will return a new string like this
*length of the number in strings* + string + *length of the number in strings*
To be more specific here's an example :
If I call the function like this
pullSubstring("for", "John Doe in the name used for people with no identity", 5)
the result will be like "used for peop".
You can use this function:
function pullSubstring(searched,text,bordersLenght) {
var idx = text.indexOf(searched);
if (idx == -1 )
return "";
var startFrom = idx-bordersLenght;
var endAt = idx + bordersLenght + searched.length;
if (startFrom < 0)
startFrom=0;
if (endAt > text.length-1)
endAt = text.length-1;
return text.substring(startFrom,endAt);
}
You have to choose what to return when the searched string is not found (my function return an empty string), or when adding n character before or after the found text will take you before the start or after the end of text (my function trim the returned text in these two cases).