From String to Object [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I need to go from
'IsEditOnly=False;IsLastFieldInRow=False;IsReadOnly=False;Title=bedrijfStandaard;Width=100'
to something like
{IsEditOnly: false, Title: 'bedrijfStandaard'}
Thanks

var s='IsEditOnly=False;IsLastFieldInRow=False;IsReadOnly=False;Title=bedrijfStandaard;Width=100';
s=s.replace(/=/g, ':"');
s=s.replace(/;/g, '",');
s+='"';
eval('var obj={'+s+'}');
alert(obj.Width);
http://jsfiddle.net/w1yq4vtc/

You could try this:
var queryToObject = function(s){
var
i = 0,
retObj = {},
pair = null,
qArr = s.split(';');
for (; i < qArr.length; i++){
pair = qArr[i].split('=');
retObj[pair[0]] = pair[1];
};
return retObj;
};
var query = 'IsEditOnly=False;IsLastFieldInRow=False;IsReadOnly=False;Title=bedrijfStandaard;Width=100';
console.dir(queryToObject(query));
JSFiddle

Related

JS Sorting an array of alphanumeric strings by ending numbers? [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 1 year ago.
Improve this question
Can someone please show me how to sort this
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/8/video-109.mp4"]
into this
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/8/video-109.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4"]
it should be sorted based on last numbers in string before .mp4 but it doesn't work
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/8/video-109.mp4"
]
videos.sort(function(a, b) {
var spix = a.split('-')[1];
a = parseInt(spix.split('.mp4')[0]);
var spixb = b.split('-')[1];
b = parseInt(spix.split('.mp4')[0]);
return a - b;
});
console.log(videos);
videos.sort((a,b) => a.split('-')[1].slice(0,-4) - b.split('-')[1].slice(0,-4));
console.log( videos)
Basically I'm just splitting it by the "-" then taking out the '.mp4' and sorting it
Your issue is with the defenition of variable b. You defined it incorrectly as b = parseInt(spix.split('.mp4')[0]); it should be b = parseInt(spixb.split('.mp4')[0]);
var videos = ["https://example.net/download/7/video-104.mp4",
"https://example.net/download/7/video-105.mp4",
"https://example.net/download/7/video-106.mp4",
"https://example.net/download/7/video-108.mp4",
"https://example.net/download/7/video-110.mp4",
"https://example.net/download/7/video-112.mp4",
"https://example.net/download/7/video-114.mp4",
"https://example.net/download/8/video-107.mp4",
"https://example.net/download/8/video-109.mp4"];
videos.sort(function (a, b) {
var spix = a.split('-')[1];
a = parseInt(spix.split('.mp4')[0]);
var spixb = b.split('-')[1];
b = parseInt(spixb.split('.mp4')[0]);
return a - b;
});
console.log(videos)

Why doesn't my javascript regexp work with array items? [closed]

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 8 years ago.
Improve this question
var x = new Array('1','2','3');
var y = new Array('a','b','c');
var iLen = x.length;
var s = 'abcdefgabcdefg';
for (var i=0;i<iLen;i++) {
var re = new RegExp(x[i],'g');
s = s.replace(y[i], re);
}
alert(s);
I want the result to be 123defg123defg.
Instead, I get /1/g/2/g/3/gdefgabcdefg.
You are doing it wrong, because you want to replace occurrences of array y to occurrences of x globally, you should say like
for (var i=0;i<iLen;i++) {
var re = new RegExp(y[i],'g'); //this is regexp for global y[i]
s = s.replace(re, x[i]); //replace all occurrences of y[i] with x[i]
}

Dont know why this variable will not work [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 am currently using
var hexArray = ["hexa", "hexb", "hexc", "hexd", "hexe", "hexf", "hexg", "hexh", "hexi", "hexj", "hexk", "hexl", "hexm"];
var burnArray = ["burna", "burnb", "burnc", "burnd", "burne", "burnf", "burng", "burnh", "burni", "burnj", "burnk", "burnl", "burnm"];
for(var i=0; i < hexArray.length; i++){
document.getElementById(hexArray[i]).className='transtart ' + burnArray[i];
};
And this is working just fine, however when I change it to this:
var hexSelect = document.getElementById(hexArray[i]);
var hexArray = ["hexa", "hexb", "hexc", "hexd", "hexe", "hexf", "hexg", "hexh", "hexi", "hexj", "hexk", "hexl", "hexm"];
var burnArray = ["burna", "burnb", "burnc", "burnd", "burne", "burnf", "burng", "burnh", "burni", "burnj", "burnk", "burnl", "burnm"];
for(var i=0; i < hexArray.length; i++){
hexSelect.className='transtart ' + burnArray[i];
};
It no longer works. And I have no idea why.
Look at your top line:
var hexSelect = document.getElementById(hexArray[i]);
It wont compile, because you use the i-variable from the for-loop.
var hexSelect = document.getElementById(hexArray[i]);
var hexArray = ["hexa", "hexb", "hexc", "hexd", "hexe", "hexf", "hexg", "hexh", "hexi", "hexj", "hexk", "hexl", "hexm"];
var burnArray = ["burna", "burnb", "burnc", "burnd", "burne", "burnf", "burng", "burnh", "burni", "burnj", "burnk", "burnl", "burnm"];
for(var i=0; i < hexArray.length; i++){
hexSelect.className='transtart ' + burnArray[i];
};
In the first line of your code you are referencing the hexArray variable which at this point is undefined. The i variable at this point is also undefined.
To get your code working all you need to do is move your first line of code into the first line of the for loop below. Does that work?

Undefined var error in an array iteration on a page using mootools [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 8 years ago.
Improve this question
var makeModelYearSelect = document.getElementById("<%= MakeModelYearFilterLB.ClientID %>").control;
var selectedMakeModelYearItems = makeModelYearSelect.get_checkedItems();
var selectedMakeModelYearItemIds = [];
for (var index = 0; index < selectedMakeModelYearItems.length; index++) {
selectedMakeModelYearItemIds.push(selectedMakeModelYearItem[index].get_value(index));
}
Why is this firing back an error of Microsoft JScript runtime error: 'selectedMakeModelYearItem' is undefined?
Mootools won't let me use a simple for...in for iterations.
I've looked at it 6 ways to Sunday so what the heck am I missing?
Because selectedMakeModelYearItem is undefined.
selectedMakeModelYearItems isn't, though.
Maybe you try to call this code berofe page is loaded. In this case select tag that you try to access don't rendered and cannot be accessed from JavaScript. You can try something like
window.addEventListener("load",
(function() {
return function setMakeModelYearFilter() {
var makeModelYearSelect = document.getElementById("<%= MakeModelYearFilterLB.ClientID %>").control;
var selectedMakeModelYearItems = makeModelYearSelect.get_checkedItems();
var selectedMakeModelYearItemIds = [];
for (var index = 0; index < selectedMakeModelYearItems.length; index++) {
selectedMakeModelYearItemIds.push(selectedMakeModelYearItem[index].get_value(index));
}
window.removeEventListener('load', setMakeModelYearFilter, false);
}})()
, false);

What happens in the execution context when this script is executed [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 8 years ago.
Improve this question
When i execute the following script the alert statement is printing the function why is it so?
What happens in the execution context? Why the variable basicPattern's undefined value is not printing?
function basicPattern(){
var o = 5;
return o;
}
var basicPattern;
console.log(basicPattern);
function basicPattern(){
var o = 5;
return o;
}
var basicPattern;
console.log(basicPattern);
Evaluates same as this (IE bugs disregarded):
var basicPattern;
basicPattern = function basicPattern(){
var o = 5;
return o;
};
console.log(basicPattern);
Since basicPattern was already declared, declaring it again won't have any effect since declarations
are hoisted and merged. If you had assignment to 5 it would go like this:
var basicPattern;
basicPattern = function basicPattern(){
var o = 5;
return o;
};
basicPattern = 5;
console.log(basicPattern);
Read more about hoisting: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

Categories

Resources