This question already has answers here:
How to convert string as object's field name in javascript
(5 answers)
Closed 7 years ago.
I have an array that I want to loop with a for.
for(var j = 0; j < outPutData.length; j++)
{
if(outPutData[j].pregunta1==1) { }
}
pregunta1, pregunta2, etc. are some of the variables.
If I print the value of outPutData[j].pregunta1 with an alert() it shows the correct value, but when I try:
for(var j = 0; j < outPutData.length; j++)
{
if(outPutData[j].pregunta+j==1) { }
OR
if(outPutData[j].pregunta+""+j==2) { }
}
an error is shown. Why?
What I expect to have is
outPutData[j].pregunta1
outPutData[j].pregunta2
outPutData[j].pregunta3
... (more till 200)...
outPutData[j].pregunta200
What am I doing wrong?
Thanks!
Try this:
outPutData[j]['pregunta' + j]
you can access properties of object by string using []:
var obj = {prop: "value"}
var a = obj.prop;
// is the same as
var b = obj['prop'];
Related
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Returning only certain properties from an array of objects in Javascript [duplicate]
(5 answers)
Javascript Array of objects get single value [duplicate]
(3 answers)
Construct an array of elements from an array of objects? [duplicate]
(2 answers)
Closed 4 years ago.
How to save javascript data from a loop to an array?
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
}
Insert the data in the array using push
var arr=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
arr.push(h);
}
var data = [];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
data.push(h)
}
//OR
var data = jsonData.Data.Positions.map(item => item.Oid);
Your variable jsonData.Data.Positions is probably already an array.
Use .push() method to add values to array.
var h=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
h.push(jsonData.Data.Positions[i].Oid);
}
console.log(h);
You can do it within a loop:
var array = []
for (i = 0; i < jsonData.Data.Positions.length; i++) {
array.push(jsonData.Data.Positions[i].Oid);
}
Or in a more functional-way:
var array = jsonData.Data.Positions.map(p => p.Oid)
map instead of for loop
var h = jsonData.Data.Positions.map(function (x) { return x.0id });
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
I'm very new to Javascript, and I could use some help troubleshooting.
In the console log it says "upper" and "wordcount" are not defined. The goal of this function is to loop through an array derived from an inputdiv and ask if the array value is present in that array, AND is NOT present in the "wordcount" array, and if it's not, to push it in.
function processtext() {
var textindiv = document.getElementById("inputdiv").innerHTML;
var split = textindiv.split(" ");
var upper = [];
var wordcount = [];
for (var i = 0; i < split.length; ++i) {
upper.push(split[i].toUpperCase());
}
var sortedlist = upper.sort();
var wordcount = new Array;
for (var i = 0; i < sortedlist.length; ++i) {
if (sortedlist.indexOf(sortedlist[i]) > -1) {
if (wordcount.indexOf(sortedlist[i]) == -1) {
wordcount.push(sortedlist[i]);
}
}
}
}
console.log(upper);
console.log(wordcount);
The variables are defined in the function, not globally.
You may either move the console.log statements into the function, or move the variable declarations out of the function.
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 6 years ago.
I tried something like:
var diff = $(array1).not(array2).get();
console.log(diff); // store the difference
And replace .not() with .is() but that didn't work..which generated an error. It only stored the difference, but I want only the same values stored in a new array. How do I do that in jQuery regardless if it's length size in both arrays?
var array1 = ['a','b','c','d','e','f','g'];
var array2 = ['c', 'b'];
var sameValArr = [];
// TODO:
// 1. compare the two arrays if there's any matching values in both
// 2. if yes, store the matching value in a new array; if no, do nothing
// 3. check the new array if it isn't empty
// 4. if empty, hide the video; if not empty do nothing (show video)
for(var i = 0; i < array1.length; i++)
{
for(var j = 0; j < array2.length; j++)
{
if(array1.indexOf([i]) === array2.indexOf([j]))
{
sameValArr.push(array1[i]);
console.log("sameValArr: ", sameValArr);
}
}
}
The answer provided to this question using indexOf() method didn't work.
for(var i=0; i< array1.length; i++)
{
for(var j=0; j< array2.length; j++)
{
if(array1[i] === array2[j])
sameValArr.push(array1[i]);
}
{
This question already has answers here:
Swap rows with columns (transposition) of a matrix in javascript [duplicate]
(5 answers)
Javascript equivalent of Python's zip function
(24 answers)
Closed 8 years ago.
how to dynamically convert this type of array:
[
[a,b,c],
[d,e,f],
]
into
[
[a,d],
[b,e],
[c,f],
]
the length of the first array is not always the same size.
tried the following
for (var i = 0; i < multi.length; i++) { // 2
for (var j = 0; j < multi[i].length; j++) { // 3
multi2[j].push(multi[j][i])
}
}
it does not work
Two issues:
Initialize your multi2 subarray for i.
You have your i and j mixed up in the inner loop.
Here's a fiddle
var multi = [
["a","b","c"],
["d","e","f"],
["g","h","i"],
]
var multi2 = [];
for (var i = 0; i < multi.length; i++) { // 3
for (var j = 0; j < multi[i].length; j++) { // 3
multi2[j] = multi2[j]||[]; // initialize subarray if necessary
multi2[j].push(multi[i][j])
}
}
This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
Is it possible to use an array variable value as a new variable name in a for-loop (or in other words, can the left-hand side of an equation be determined by a pre-defined value in an iteration)?
for (var i = 0; i < array.length; i++) {
// we all know this is possible:
blabla[i] = apple;
// but I'm wondering if there's a way we can achieve this:
example(someVar);
}
function example(name) {
// [name] = banana;
name = banana;
}
Obviously, the way I'm doing this in the snippet above, the value banana is always getting assigned to the variable name. Not too sure though how I could go about this?
Variables are properties, either of the global object (in a browser: window) or of some other object. So you can do:
for (var i = 0; i < array.length; i++) {
window[array[i]] = '[something]';
}
or
var someObj = {};
for (var i = 0; i < array.length; i++) {
someObj[array[i]] = '[something]';
}
function myVar(val) {
this.value=val;
}
var array=new Array( new myVar(1),new myVar(2),new myVar(3));
for (var i = 0; i < array.length; i++) {
example(array[i]);
alert(array[i].value); // now they are 0,0,0
}
function example(name) {
name.value = 0;
}