I push two Array in one Array But can't use them as a string,This result in bellow about two arrays in one array :
Array(0) [] //This is empty array that I create before and push these two array bellow to them
length:2
0:Array(1) ["cfdb9868-0f69-5781-b1e4-793301280788"]
1:Array(1) ["cfdb9868-0f69-5781-b1e4-793301280788"]
and I create a for for access them but I can ! I write this code "
for(var index = 0 ; index < Array.length ; ++index) {
let Each_String_In_Brackets = Array[index] ;
console.log(Each_String_In_Bruckets);
}
Why is this happen!
I mean Why when we push array in empty array can't access them!
I want to access the content of them, I have a string In each bracket.
var arr = [];
arr.push(["cfdb9868-0f69-5781-b1e4-793301280788"]);
arr.push(["cfdb9868-0f69-5781-b1e4-793301280788"]);
//assuming inside array always will be one element:
arr.forEach((item)=>{ console.log(item[0])})
//if inside array may be multiple elements, then use this
arr.forEach((item, index)=>{
item.forEach((child)=>{ console.log(child)})
})
pushing a full array into another array makes it a 2D array ( at the indexes where you push another array ) so for example if I have the first array
BArray[]
But if I push another array into it
BArray2 = [1,2,3,4];
BArray.push(Array2);
We would not be able to access it just by
BArray[0]
This would return the entire array2 rather the content of array2 at index 0.
Therefore you would do this
BArray[0][0]
So this would give us ( from your array ) "cfdb9868-0f69-5781-b1e4-793301280788"
If you would like to just dump out the content of BArray2 into BArray
You can use the spread operator.
BArray[...BArray2];
( Also I would not use Array as variable name ! It can be confusing as new Array(10); is a way of creating arrays and having arrays with that name isn't best practice ! )
Hope this helps !
You are pushing array in array, so you must access as 2D array:
var array = [];
var arrayString1 = ["StringInArray1"],
arrayString2 = ["StringInArray2"];
array.push(arrayString1);
array.push(arrayString2);
console.log(JSON.stringify(array));
array.forEach(arrayItem => {
console.log("StringInArray: " + arrayItem[0]);
})
Or maybe you want to append array:
var array = [];
var arrayString1 = ["StringInArray1"],
arrayString2 = ["StringInArray2"];
[].push.apply(array, arrayString1);
[].push.apply(array, arrayString2);
console.log(JSON.stringify(array));
array.forEach(arrayItem => {
console.log("StringInArray: " + arrayItem);
})
Related
I'm getting a array like this
var cars = [['BMW'],[],['6000cc']];
which contains two different values of a same instance. Here you can see an empty array that's where the data changes. Basically the left side of the empty array contains one set of data and right side of the empty array contains other set of data.
I need to split this array in to two.
So you need to split array into two which is separated by empty array.
First you have to find the index of empty array.
var cars = [['BMW'],[],['6000cc']];
var index = -1
for(var i=0;i<cars.length;i++){
if(cars[i].length === 0)
{
index = i;
break;
}
}
then you can use slice to split array
var arr1 = cars.slice(0, index);
var arr2 = cars.slice(index+1);
You can use ES6 Array De-structuring and split your arrays.
let cars = [['BMW'],[],['6000cc']];
let [cname, other, cpower] = cars;
console.log(cname, cpower);
I want to create array once and then just push values to it with any index , but i get Cannot read property 'push' of undefined error
I have following scenario
var neg = [];
I want to use push randomly with any index
neg[0].push([1,2]);
or
neg[22].push([1,2]);
right now I want to define manually like neg[0] = []; , Is there any one way where i can just push to any index i want ?
Here's a quick way to do exactly what you want.
var arr = [];
(arr[0]=[]).push([1,2]);
console.log(arr)
Also it's safer to check if an array already exists at this index, otherwise it will be replaced with an empty array – the above code will replace the array with an empty one if it already exists.
// creates an array at index 0 only if there's no array at this index.
// pushes an array of [1, 2] to the existing array at 0 index
// or the newly created empty one.
(arr[0] || (arr[0] = [])).push([1, 2]);;
var arr = [];
arr[0] = [1];
(arr[0]||(arr[0]=[])).push(2,3);
console.log(arr)
Push will add elements to the end of an array. For example:
var neg = [];
neg.push(3); //this will add the number 3 to the end of your array
The problem here is your trying to add a value to an undefined element:
var neg = [];
neg[0].push([1,2]); // <-- neg[0] is not defined at this moment, and you're treating it as an array object.
If you define your elements as an array, you will not get such an error:
var neg = [];
neg[0] = [];
neg[22] = [];
neg[0].push([1,2]);
neg[22].push([1,2]);
Alternatively (and this is probably what you're probably looking for), you can set the value for any element of your array with the desired index:
var neg = [];
neg[0] = [1,2];
neg[22] = [1,2];
You are going to have to set it to an empty array before you can push it.
neg[0] = neg[0] || []
neg[0].push([1,2]);
You can specify how large the array will be if you know ahead of time, for example var neg = new Array(40); will create a new array with 40 undefined elements.
Once that is created, you can change the value at any index as long as 0 <= index <= 39.
You can use array.splice(your_index, 0, your_element); to push an element into an array at the given index.
I use this method to find object in array:
lat arr = [];
found = this.obj[objKey].filter(item => item[internKeyName] == 7047);
arr.push(found);
Problem is that if element was not found it added this as undefined to array arr. How avoid this?
Why it does not find element with key: "subjectId":
let objKey = 7047;
let k = "subjectId";
let v = 7047;
found = this.obj[objKey].filter(item => item[k] == v);
console.log(found);// undefined
You can avoid this by checking the length found before you push it to the array.
lat arr = [];
found = this.obj[objKey].filter(item => item[internKeyName] == 7047);
found.length > 0 && arr.push(...found);
I am using the spread syntax to push each element as its own item to the new array, which I assume that is what you want. You can remove the ... if you want all of the found items to be its own array item.
The function filter won't return undefined, will return an empty array instead (if none elements met the condition).
Problem is that if element was not found it added this as undefined to array arr.
You probably want to find a specific element, so, I recommend you to use the function find if you want only one object rather than an Array with only one index.
lat arr = [];
found = this.obj[objKey].find(item => item[internKeyName] == 7047);
if (found) arr.push(found);
You could push a spreaded array with the wanted objects directly, empty arrays are not spreaded (spread syntax ...).
arr.push(...this.obj[objKey].filter(item => item[internKeyName] == 7047));
I have array, created from json:
var array = [{"name":"name1","group":"group1","id":"123", ...},
{"name":"name2","group":"group2","id":"456", ...},
{"name":"name3","group":"group1","id":"789", ...}];
After I get another array:
var array1 = [{"name":"name1","group":"group1","id":"123", ...},
{"name":"name4","group":"group1","id":"987", ...}]
I need to push items from second array into first, but how can I check if first array contains objects from second array?
Each object in array contain more property and some of them are created dynamically so I can't check for example by indexOf(). All solutions that I found works only with simple objects like Int. It will be great if I could check by property "id" for example.
Use find first
var newObj = {"name":"name2","group":"group2","id":"456"};
var value = array.find( s => s.id == newObj.id );
Now push if the value is not found
if ( !value )
{
array.push( newObj )
}
(More generic)you can do this one line using following (which will add all object which is not in array).
array.concat(array1.filter(x=>!array.find(s=>s.id==x.id)));
var array = [{"name":"name1","group":"group1","id":"123"},
{"name":"name2","group":"group2","id":"456" },
{"name":"name3","group":"group1","id":"789"}];
var array1 = [{"name":"name1","group":"group1","id":"123"},
{"name":"name4","group":"group1","id":"987"}];
array=array.concat(array1.filter(x=>!array.find(s=>s.id==x.id)));
console.log(array);
I made a multidimensional array today which creates 4 new arrays within the first array. when I console.log my array it says that there are 0 items in it, but i do see my 4 arrays each with other items. See the console.log below for a clearer image:
vm.allGroupsInClassifications = [];
datacontext.graph.getAllGroups().then(function (data) {
var groups = [];
// get all clasification names and put them in the array and create a new array
for (var k in vm.classificationNames) {
var groupName = vm.classificationNames[k];
groups[groupName] = new Array();
}
// go through all the groups and sort them based on their classification
for (var i = 0; i < data.length; i++) {
if (data[i].classification != null) {
modifyGroupContent(data[i], groups, 0);
}
else if (data[i].classification == null)
modifyGroupContent(data[i], groups, 1);
}
vm.allGroupsInClassifications = groups;
console.log(vm.allGroupsInClassifications);
any help would be appreciated. Cheers!
Because you have no indexes. groups[groupName] = new Array(); won't add an item to your array, because all your groupName variables are not numbers. And thus you have an array-object-like thing.
If your groupName's were say 0,1,2 and 3, when you console.log the object, you will get the last number+1 (in this case 4).
Here you can access your arrays with the keys - so in this case vm.allGroupsInClassifications['Confidential'] will return your array.
But since you don't have actual numeric indexes, the length of your object-array thing is 0.
Hope you understand
It's because your array is incorrect. You have an array with keys :
[
"Algemeen" : [...],
"Confidential" : [...]
]
This is incorrect, as arrays can't have keys. Javascript being Javascript, it allows you to define it nonetheless, but when you ask for its length, it's quite unable to answer and says 0. It should be an object :
{
"Algemeen" : [...],
"Confidential" : [...]
}
If you really want to keep an array of arrays, remove the keys :
[ [...],[...] ]
Now the length is 2.