Push String into Array in Array at certain position - arrays

How can I push a String into an array that is in an array?
var outerArray = [[]]
outerArray.push('test1')
outerArray.push('test2')
outerArray[0] = outerArray[0].push('test3')
console.log(outerArray[0][0])
this just returns undefined

It seems like you want to create an array at position 0 including the previous item at that index and a new one. This will work:
outerArray[0] = [outerArray[0], 'test3']

const innerArray = outerArray[0];
const pushedString = innerArray.push('testString');
Also, as you know, push returns an item that has been pushed.

Related

Adding an Array to an Array in Angular

So I have a db document that holds some string values in an array, I want to push just the array from every entry into an array in the application for usage later, But I can see the array fine on the fetch, and when I iterate it but my "Global array" is staying empty can someone explain why?
specialDates : Specialdates[] = [];
specialRange: any[] = [];
this.specialDates.forEach(ag => {
//ag,range -> I can see fine
this.specialRange.push(ag.range);
//this.specialrange -> Stays empty
});
Array looks something like the following:
1205,1206,1207,1208
What is wrong with this approach?
Reason for doing this is because the documents have 2 fields minimum: EG ->
ID/Array
And I just need the array
this.specialRange = this.specialDates.map(ag => ag.range)

Remove random item from array and push to new array

Thank you in advance! I'm new to coding and learning how to deal with Arrays. I am trying to remove random items from an array (deck of cards), and populate a new array (called hand). The problem I always seem to have with arrays is taking the results of one and creating a new function/array/ etc.. Currently, I am outputting 2 separate arrays and I can't seem to push them into one.
let deck = ["dA","dQ","dK","dJ","d10","d09","d08",
"d07","d06","d05","d04","d03","d02","hA","hQ","hK",
"hJ","h10","h09","h08","h07","h06","h05","h04","h03"];
var hand = deck.splice(Math.floor(Math.random()*deck.length),1);
console.log(hand)
var hand = deck.splice(Math.floor(Math.random()*deck.length),1);
console.log(hand);
In your code, just you need to push the value returned from splice method rather than directly assigning it.
By this way every time a new value that gets deleted and will be added to the new array called hand. Hope this helps. :-)
let deck = ["dA","dQ","dK","dJ","d10","d09","d08",
"d07","d06","d05","d04","d03","d02","hA","hQ","hK",
"hJ","h10","h09","h08","h07","h06","h05","h04","h03"];
var hand = [];
const getSelectedCard = () => deck.splice(Math.floor(Math.random()*deck.length),1)
let selectedCard = getSelectedCard();
hand.push(...selectedCard) //or hand.push(selectedCard[0])
console.log(hand);
selectedCard = getSelectedCard();
hand.push(...selectedCard)
console.log(hand);

Angular saving default array

I have this code:
$scope.DefaultSidebarLinks = [
{
"Link":"/home",
"Title":"Home",
"Icon":"fa-home"
}
];
$scope.SidebarLinks = $scope.DefaultSidebarLinks;
$scope.addSidebarLink = function(link,title,icon,resetFirst){
var element = {"Link":link,"Title":title,"Icon":icon};
if(resetFirst)
{
$scope.SidebarLinks = $scope.DefaultSidebarLinks;
$scope.SidebarLinks.push(element);
}
else
$scope.SidebarLinks.push(element);
}
The main problem is that when I push a new element in SidebarLinks, it pushes it also in DefaultSidebarLinks.
What I'm trying to do is to reset the SidebarLinks if asked and push the new given element only in this local variable.
Wen you assign an array like this :
$scope.SidebarLinks = $scope.DefaultSidebarLinks;
you are creating two arrays which are same i.e two different names of same array . They are pointing to same memory , so any change in $scope.SidebarLinks will also change the array $scope.DefaultSidebarLinks as these both are same .
If you want deep copy an array you can do it by many ways :
Solution1(Angular way):
$scope.SidebarLinks=angular.copy($scope.DefaultSidebarLinks);
Solution2 (javascript way):
$scope.SidebarLinks = $scope.DefaultSidebarLinks.slice();
Basically, the slice() operation clones the array and returns the reference to the new array.

How to save JSON objects into an array

i want to save objects into an array. I have JSON objects and I want to save every object in an array to access every element alone.
Can anybody help me?
toArray = JSON.parse(res.body)
categ = Array.new
i = 0
toArray.each do |object|
newMyObject = MyObject.new(object)
categ = Array.new(i, newMyObject)
i = i+1
end
Try this one
array_from_json = JSON.parse(res.body)
objects_array = array_from_json.map { |item| MyObject.new(item) }
The issue in your code is that you are creating a new array every iteration.

swift - using .map on struct array

i have a struct array that i want "break up" into smaller arrays that can be called as needed or at least figure out how i can map the items needed off one text value.
the struct:
struct CollectionStruct {
var name : String
var description : String
var title : String
var image : PFFile
var id: String
}
and the array made from the struct
var collectionArray = [CollectionStruct]()
var i = 0
for item in collectionArray {
print(collectionArray[i].name)
i += 1
}
printing partArray[i].name gives the following result:
pk00_pt01
pk00_pt02
pk00_pt03
pk01_pt01
pk01_pt02
pk01_pt03
pk01_pt04
pk01_pt05
pk01_pt06
pk01_pt07
pk01_pt08
this is just some test values but there could be thousands of entries here so i wanted to filter the entire array just by the first 4 characters of [i].name i can achieve this by looping through as above but is this achievable using something like .map?
I wanted to filter the entire array just by the first 4 characters of
[i].name
You can achieve this by filtering the array based on the substring value of the name, as follows:
let filteredArray = collectionArray.filter {
$0.name.substring(to: $0.name.index($0.name.startIndex, offsetBy: 4)).lowercased() == "pk00"
// or instead of "pk00", add the first 4 characters you want to compare
}
filteredArray will be filled based on what is the compared string.
Hope this helped.
If you want to group all data automatically by their name prefix. You could use a reducer to generate a dictionary of grouped items. Something like this:
let groupedData = array.reduce([String: [String]]()) { (dictionary, myStruct) in
let grouper = myStruct.name.substring(to: myStruct.name.index(myStruct.name.startIndex, offsetBy: 4))
var newDictionart = dictionary
if let collectionStructs = newDictionart[grouper] {
newDictionart[grouper] = collectionStructs + [myStruct.name]
} else {
newDictionart[grouper] = [myStruct.name]
}
return newDictionart
}
This will produce a dictionary like this:
[
"pk00": ["pk00_pt01", "pk00_pt02", "pk00_pt03"],
"pk01": ["pk01_pt01", "pk01_pt02", "pk01_pt03", "pk01_pt04", "pk01_pt05", "pk01_pt06", "pk01_pt07"],
"pk02": ["pk02_pt08"]
]
Not sure if i am understanding you correctly but it sounds like you are looking for this...
To create a new array named partArray from an already existing array named collectionArray (that is of type CollectionStruct) you would do...
var partArray = collectionArray.map{$0.name}

Resources