Get the value of the array index [closed] - javascript

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 a ajax response array and I want get the value of there index.
[{"UserLatitude":"33.7543","UserLongitude":"-84.3744"}{"UserLatitude":"22.6962","UserLongitude":"75.8651"},{"UserLatitude":"22.6963","UserLongitude":"75.8654"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"22.6962","UserLongitude":"75.8653"},{"UserLatitude":"22.6963","UserLongitude":"75.8654"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"33.7543","UserLongitude":"-84.3745"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"33.7543","UserLongitude":"-84.3744"}]

You're missing a comma after the first array object which is causing an error. Otherwise result[0].UserLatitude will work - jsfiddle.

You need to iterate the array and then check those values are matching like
$.each (arr, function (index, value) {
if(value["UserLatitude"] == "33.7543" && value["UserLongitude"] == "-84.3744") {
console.log(index);
return false;
}
});
if matches found, print the index and exit the loop.
Fiddle

Related

Javascript parse JSON string [closed]

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
Folks, DynamoDB call returns a JSON Object, which I would like to parse, and grab the password hash field
jsonString = JSON.stringify(data)
console.log(jsonString)
output:
{"Count":1,"Items":[{"token":{"S":"token"},"uid":{"S":"33c02130-66b5-11e3-bdb0-7d9889f293b5"},"password":{"S":"$2a$10$ervzJ.DWjHOXRtJSugTaWuquI2OvPLyipa4YXecc/2KdQnmhrHxr6"},"username":{"S":"foo"},"plate":{"S":"dinner"},"name":{"S":"Test Name"},"server":{"S":"bar"}}]}
How would i parse this string, and retrieve the 'password' field?
The following code does not work:
console.log(jsonString.password)
console.log(jsonString.uid)
The following returns undefined:
console.log(data.password);
Thanks!
This is already an object, so you can do this:
var str = {"Count":1,"Items":[{"token":{"S":"token"},"uid":{"S":"33c02130-66b5-11e3-bdb0-7d9889f293b5"},"password":{"S":"$2a$10$ervzJ.DWjHOXRtJSugTaWuquI2OvPLyipa4YXecc/2KdQnmhrHxr6"},"username":{"S":"foo"},"plate":{"S":"dinner"},"name":{"S":"Test Name"},"server":{"S":"bar"}}]};
alert(str.Items[0].password.S);

how to get the integer value from javascript variable [closed]

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 following function
function answerQ(value) {
var newVal = value;
alert(newVal);
}
and the input button
<input type="button" onClick="answerQ(this.value)" value="Answer Q2">
how can I get just the integer "2" out of this??
Remove anything that isn't a number
function answerQ(value) {
var newVal = +value.replace(/\D/g,'');
alert(newVal);
}

Get text in getElementsByClassName [closed]

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 am writing some javascript to be run in a bookmarklet that should get the text within a element that has a certain class name.
So for example
document.getElementsByClassName('price')
where the web page has or example
<span class="price">
£23
</span>
How would I go about getting the actual text within the the element that has a class name price (i.e £23 in the above example).
getElementsByClassName returns an array of elements. Traverse through that array and get the innerText property of each. For example:
var priceEls = document.getElementsByClassName("price");
for (var i = 0; i < priceEls.length; i++) {
var price = priceEls[i].innerText;
alert("Price: " + price);
}
Live demo: http://jsfiddle.net/YQsBW/

Improving code for learning purposes [closed]

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 three tabs and I want to navigate when I click on each one of them. The code that I wrote is working just fine, but I believe that is bad coding, there is any way to improve this is just for learning purposes. Thanks!!!!
jQuery(".nuestra_actualidad li:eq(0)").click(function() {
jQuery("#tabs-actualidad").css("display","block");
jQuery("#tabs-articulos").css("display","none");
jQuery("#tabs-noticias").css("display","none");
});
jQuery(".nuestra_actualidad li:eq(1)").click(function() {
jQuery("#tabs-actualidad").css("display","none");
jQuery("#tabs-articulos").css("display","block");
jQuery("#tabs-noticias").css("display","none");
});
jQuery(".nuestra_actualidad li:eq(2)").click(function() {
jQuery("#tabs-actualidad").css("display","none");
jQuery("#tabs-articulos").css("display","none");
jQuery("#tabs-noticias").css("display","block");
});
Replacing jQuery with $ if possible (unless it clashes with another library) and then it can be reduced to a single function by utilising the index of the clicked element and calling the toggle function:
$(".nuestra_actualidad li").click(function() {
var index = $(this).index();
$("#tabs-actualidad").toggle(index === 0);
$("#tabs-articulos").toggle(index === 1);
$("#tabs-noticias").toggle(index === 2);
});
Example - http://jsfiddle.net/gSKeL/

How to replace occurrences by key [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
This is a simple question about JavaScript,
Say I got the following string:
A)My Name B)My Name C)My Name
now I give to a function the key #1 expecting to replace the second occurrence of My Name inside that function and return:
A)My Name B) C)My Name
I haven't found the solution to this anywhere online so I'm asking.
You could use split to separate the string into the parts, then join the before and after back together:
function removeNthMatch(input, removeString, removeIndex) {
var splitString = input.split(removeString);
result = splitString.slice(0, removeIndex + 1).join(removeString)
+ splitString.slice(removeIndex + 1).join(removeString);
}
input = "A)My Name B)My Name C)My Name";
removeString = "My Name";
removeIndex = 1;
console.log(removeNthMatch(input, removeString, removeIndex));

Categories

Resources