I have a collection of $linesheetItems, now I need to loop these $linesheetItems inside a foreach loop and store a seasons array by using line sheet item's season code ($linesheetItem['season']). But according to my current code, it returns an empty array.
Code:
$seasons = [];
foreach($linesheetItems as $linesheetItem) {
$seasons = Season::where('code', $linesheetItem['season'])->get();
}
dd($seasons);
How to achieve this, and what are the modifications should I do to my code?
In your code, you are overriding the $seasons variable each time the loop runs. In order to add an item to an array you have to set $seasons[] = Season::where('code', $linesheetItem['season'])->get();. This will always push a new item into the array. If you want to have custom keys on the array, you can do $seasons['your-key'] = Season::where('code', $linesheetItem['season'])->get();
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);
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 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.
scala noob here, i have a collection (Seq) of xml nodes, and i would like to populate an Array based on each node:
val nodes = data.child \\"package"
var packages = new Array[Package](nodes.length)
var index = 0
for(val entry <- nodes) {
packages(index) = new Package(entry)
index = index+1
}
Although it works, does not look much "scala-ish" to me, and i'm sure there's a better way to do it..
Any ideas?
(data.child \\ "package") map(new Package(_)) toArray