I have an array that loads an array with properties at a certain index:
var hotspotLocationsTextAndAudio [];
var myArr = [
[{x:10, y:40, filename:"test", text:"test"}],
[{x:50, y:60, filename:"test2", text:"test2"}]
];
hotspotLocationsTextAndAudio[window.IDNum] = myArr;
To access a property at a certain index of hotspotLocationsTextAndAudio (my case being what window.IDNum equals) at an index of the array inside that (0) and a certain property, I thought I just needed to do the following:
alert(hotspotLocationsTextAndAudio[window.IDNum][0].x);
That returns undefined.
Assuming the code you're actually using is this:
var hotspotLocationsTextAndAudio [];
var myArr = [
[{x:10, y:40, filename:"test", text:"test"}],
[{x:50, y:60, filename:"test2", text:"test2"]}
];
hotspotLocationsTextAndAudio[window.IDNum] = myArr;
The alert code you'll need is:
alert(hotspotLocationsTextAndAudio[window.IDNum][0][0].x);
The reason being that you wrapped your objects in [], putting them in an array.
Or, in order to have your original alert working, you'll need to change myArr to this:
var myArr = [
{x:10, y:40, filename:"test", text:"test"},
{x:50, y:60, filename:"test2", text:"test2"}
];
This works for me: http://jsfiddle.net/Ku2tQ/
var hotspotLocationsTextAndAudio = [];
var myArr = [
[{x:10, y:40, filename:"test", text:"test"}],
[{x:50, y:60, filename:"test2", text:"test2"}]
];
alert(myArr[1][0].x);
First of all hotspotLocationsTextAndAudio is an empty array, and you never push/merge myArr to it. So trying to access a nested value of an empty array, will fail.
arr, is also never defined.
If possible, keep it simple and try avoiding nested array with too much deep.
Related
I have to following array:
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
And I would like to convert it to an array of arrays, as follows:
var arr1 = [
["0.1","0"],
["1.1","1"],
["2.1","2"]
];
I would like to convert to the other array only a few properties, not all of them.
Any idea how I can do this?
PS: I was using flatMap from another post but it does not work as it does not exist in Edge.
Assuming that the second value in each subarray is coming from the number after the period from the m key, you could use the map function to accomplish this:
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
var newArray = arr1.map(x => [x[0].n, x[0].m.split('.')[1]]);
console.log(newArray);
For next time you have to put your attempts.
this is the solution for your problem
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
arr1 = arr1.map(currentArray=>{
const item = currentArray.shift();
return [item.n,item.m.replace( /^\D+/g, '')]
});
i have array like this
[["apple","banana"],["monkey"]];
how can i associate key to them like,
[{"fruit":["apple","banana"],"wild":["monkey"]}]
is this possible?
i'm trying something like this
var arr = [["apple","banana"],["monkey"]];
var newArray = [];
for(var i=0;i<arr.length;i++){
newArray["fruit"] = arr[i] //further code i don't know
}
help me th
While you can do that, like this:
var array = [["apple","banana"],["monkey"]];
var update = [
{fruit: array[0], wild: array[1]}
];
console.log(update);
...frankly it seems unlikely that's really want you want.
Very simply you can assign a new name simply
var array = [["apple","banana"],["monkey"]];
var names = ["fruits", "wild"]
var modified = [{names[0]: array[0], names[1]: array[1]}];
If you have a lot of values just use a for loop to iterate and assign value to it.
I can't seem to figure out as to why I am unable to append a value to my array below? Is there a special syntax to follow or something when constructing an array like in the method used below?
var arr = {
"fruits": ['apple','banana','orange']
};
arr.push({ "fruits":"testing123"}); // This line fails
alert(arr["fruits"]);
Try: arr.fruits.push("mango");
You can't push() to an object. You should either use the key-value notation:
arr.anotherFruits = "testing123"; // another key
arr.fruits = "testing123"; //to overwrite
Or make arr actually array:
var arr = [
{"fruits": ['apple','banana','orange']}
]
arr.push({ "fruits":"testing123"})
alert(arr["fruits"])
In this case you'd get
var arr = [
{"fruits": ['apple','banana','orange']},
{"fruits":"testing123"}
]
Or in case you did want to get an object like this
var arr = {
"fruits": ['apple','banana','orange','testing123']
}
you should've used arr.fruits.push('testing123');
Your array defintion is all wrong. Assuming you want arr to be an object that has an array under fruits and then push another value inside fruits, use the following:
var arr = {
fruits: ['apple', 'banana', 'orange']
};
arr.fruits.push("testing123");
console.log(arr["fruits"]);
Change your initial arr declaration to this...
var arr = [{
"fruits": ['apple','banana','orange']
}];
Then you can push more objects onto it. E.g.
arr.push({"fruits": ['mango', 'pear', 'tomato']});
Or, if you wish to push fruits to the existing array INSIDE your object, then simply use your existing arr declaration and use it like this...
arr.fruits.push('tomato');
arr.fruits.push('pear');
arr.fruits.push('tomato');
I'd argue that you don't really require this to be an object at all here.
All you need is this...
var fruits = [];
fruits.push('banana');
fruits.push('apple');
It's simply an array of fruits. If you need more complex structure, then use an array of objects, instead of strings.
Array has push() method, but Object does not. Here you create an Object, but not an Array. So it failed.
I'm trying to push a new array into a global array here:
var history = [ ];
function addHistory(array)
{
history.push(array);
console.log(history)//to check what is the value of array
}
var test1 = [0,0,0,0,0];
var test2 = [1,1,1,1,1];
addHistory(test1);
addHistory(test2);
After doing like this, the array should be:
[ [0,0,0,0,0] , [1,1,1,1,1] ]
But instead it prints
[ [1,1,1,1,1] , [1,1,1,1,1] ]
So basically it replaces all old arrays in array "history", instead of pushing it at the end.
What can be wrong here?
Thanks a lot
EDIT:
Sorry, forgot to mention that my actual variable is not called history (I called it that way just that you can imagine what I want). It is called "rollHist"
history is a protected term in javascript. Changing it to this will fix it:
var myHistory = [];
function addHistory(array)
{
myHistory.push(array);
console.log(myHistory)//to check what is the value of array
}
var test1 = [0,0,0,0,0];
var test2 = [1,1,1,1,1];
addHistory(test1);
addHistory(test2);
You can read more about the various protected words here
I have 3 arrays like:
var arr1 = [];
var arr2 = [];
var arr3 = [];
//When I want to add something into array then I use
arr1.push("text");
arr2.push("text");
but is it possible to make something like the following example?
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(item);
is it even possible to do something like that? I have tried but does not work.
There's a fundamental misunderstanding though, arr is an array but you're using it as an associative array, which in JavaScript is better represented with an object {}. for...in is for objects, NOT arrays, the MDN has a warning note about it:
for..in should not be used to iterate over an Array where index order
is important...
I would advice even if index is trivial to use a regular for loop or a forEach.
Consider using the following, more appropiate approach.
var obj = {
group1: ['text1'],
group2: ['text2'],
group3: ['text3']
};
// pushing more strings
obj.group1.push('foo');
obj['group2'].push('baz');
You're treating arr['group1'] as an array (by using .push()), but you haven't declared it as an array.
var arr = [];
arr['group1'] = [];
arr['group2'] = [];
arr['group3'] = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
It seems you're actually looking for Javascript Objects instead of arrays.
Also, you need to create these objects first.
var obj = {group1:[],group2:[],group3:[]};
/* or
var obj = {};
obj.group1 = [];
*/
obj['group1'].push("text1");
// or obj.group1.push("text1");
The for...in structure sets your for variable to the key, not the value. Assuming arr['group1'] is an array, this will work fine:
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(arr['group1'][item]);