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
Should be quite a common answer but I haven't found it.
Using client-side javascript:
My client receives some JSON string:
response =
[
{"id1":"value1" , "time1":"valuetime1"},
{"id2":"value2" , "time2":"valuetime2"}
]
I understand that I can simply parse the JSON string with this command:
response = JSON.parse(response);
But what is the next command to access each of the objects individually?
I would prefer to not use jQuery.
In plain JavaScript, just use a simple for loop:
for(var i = 0; i < response.length; i++){
console.log(response[i]); // Object with id and time
}
-or-
response.forEach(function(object){
console.log(object);
});
Demo
This JSON will become the appropriate JavaScript object. In this case, that is an array which you can access the same way as you would any other JavaScript array:
for (var i = 0; i < response.length; i++){
console.log(response[i]);
}
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 trying to take an array of integers and cast it to bytes in JavaScript but can't quite figure out how to do it.
The input would look something like [2,-76,7,2,8,69,82,88,87,2,52,50,...].
If I was going to do it in another language like Java I'd use something like the following.
byte[] bytArr = new byte[intArray.size()];
for (int i = 0; i < intArray.size(); i++) {
bytArr[i] = (byte) intArray[i];
}
I'm pretty new to JS so not sure if this is even possible...
You can use Int8Array, which is a typed array.
const arr = Int8Array.from([2,-76,7,2,8,69,82,88,87,2,52,50]);
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 4 years ago.
Improve this question
[1,2,3,4,5,6] should be changed to set containing {1,2},{2,3},{3,4},{4,5},{5,6} using normal for loop and using javascript map and reduce functions convert array to set of pairs using different techniques Thanks in Advance
Why not a simple for loop? Array functions are great but are often unnecessary.
const arr = [1,2,3,4,5,6]
const result = []
for (let i = 1; i < arr.length; ++i)
result.push([arr[i-1], arr[i]])
console.log(result)
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 am getting below line in my source file and I would like to sum those values separated by pipe ("|")
There is no limit for the values coming in the line (might be 100 values)
10|20|30|40|[no limit for values] - Separator is pipe "|"
The ouptput would be 100
Please help to write a javascript function for the above query.
Regards
Jay
You should take a look at JavaScript's built-in functions: With split you can split your string into an array, with reduce you can 'reduce' your array to a single value, in that case via summation. These two links should provide you enough information for building your code.
You could try something like below:
function sum(value){
var total = 0;
var array = value.split(",").map(Number);
for( var i = 0; i < array.length; i++) {
total += array[i];
}
alert(total);
}
var value = "10|20|30|40".replace(/\|/g, ',');
console.log(sum(value));
https://jsfiddle.net/f7uqw7cL/
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 6 years ago.
Improve this question
Obviously, this is not allowed in javascript:
for (var i=0; i<data.length; i++){
array[i].push(data[i].name);
}
Does anyone know of a workaround that does the same thing?
You can do
for (var i=0; i<data.length; i++){
array[i] = [];
array[i].push(data[i].name);
}
https://jsfiddle.net/zv0xfzzs/
but Alexey Ayzin, shurely this is the better alternative - create a single array, push the desired values into it and then access it via normal means::
arrayTest = [];
for (var i=0; i<data.length; i++){
arrayTest.push(data[i].name);
}
This will give arrayTest all names, and can be accessed as:
var testName=arrayTest[3];
Doing it the other way will give you lots of unnamed arrays - how are they to be used?
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
In my page there are some article tags with an attribute data-course-id.
<article class="register" data-course-id="0123"></article>
<article class="register" data-course-id="0124"></article>
Now I would like to generate a variable which contains an array from the article tags and the value should be the same as the data-course-id.
var array = [0123, 0124]
How can I do this?
Like this:
var array = $('.register').map(function(){
return $(this).attr('data-course-id'); // converting to Number will result in loss
// of data since what you are using is octal representation.
}).get();
Without jQuery, you can try something like this:
var articles = document.getElementsByClassName("register");
var array = [];
for(var i = 0; i < articles.length; i++) {
array.push(articles[i].getAttribute("data-course-id"));
}
Working demo.
try like this,
$('.register').each(function(){
arr.push($(this).attr('data-course-id'));
alert($(this).attr('data-course-id'));
});
Live Demo Here..
Using jquery, I would do something like this
Pushing the data values:
var array=[]
$(".register").each(function(){
array.push($(this).data('course-id'));
});
alert("pushing only the data values:" + array)
Pushing the objects:
array2 = []
$(".register").each(function(){
array2.push($(this))
});
alert("pushing the objects: " + array2)
http://jsfiddle.net/x9LdR/1