Angular saving default array - angularjs

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.

Related

create a new Array with filtered out props

I have been trying to filter an Array by its props and not by its value so my original array would be -
const orignalArray =[
{id: 1, name:"jim", email:"jim#mail.com",age:20},
{id: 1, name:"jom", email:"jom#mail.com",age:30}
]
id like to be able to use (n) amount of filters.
My output array would look ideally look like this
const filterList["id","age"]
const newArray=[{name:"jim", email:"jim#mail.com"},{ name:"jom", email:"jom#mail.com"}]
I have tried to use filter() but cant seem to get it to work.
any help is much appreciated.
In this case you aren't filtering the array rather creating a new one based on the original with derived elements from each. To achieve this you can use the array map function to loop over the array and create a new one with new objects without the desired properties, e.g.:
function removeArrayElementProps(arr, propsToRemove) {
return arr.map((element) => {
// Create a copy of the original element to avoid modifying the original
const newElement = {...element};
// Remove the desired properties from the new element
propsToRemove.forEach((propToRemove) => {
delete newElement[propToRemove];
});
// Returning the modified element in map puts it in thew new arry
return newElement;
});
}
Then you simply call:
const newArr = removeArrayElementProps(orignalArray, ['id','age']);
This loops over the array, creates a copy of each element, and removes the specified properties from the element.

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)

How can do array map inside methods in vuejs

How can ı equalize my array ıd and my value ıd and access value.name I didn't do it
This is my code:
activity(val) {
var act = this.items.map(function (val) {
if (element.ActivityID== val) {
return element.ActivityName
}
return act
});
Perhaps this?
activity (val) {
const activity = this.items.find(item => item.ActivityID === val)
return activity && activity.ActivityName
}
This just finds the item with the corresponding ActivityID and then returns its ActivityName.
Your original code contained several possible mistakes:
Two different things called val.
element doesn't appear to be defined.
The return act was inside the map callback. The activity method itself wasn't returning anything.
Not really clear why you were using map to find a single item. map is used to create a new array with the same length as the original array with each item in the new array determined by the equivalent item in the original array. It 'maps' the items of the input array to the items in the output array.

How to push object into an array? in Angular 7

I am pushing an object into an array but cannot do it?
I'm doing it like this
this.passData = this.tribeForm.value;
var id = {"tribe_id": 1}
this.passData.push(id)
This is the value in the tribeForm
I also tried
var id = {tribe_id: 1}
and
this.passData.splice(0,0, id)
and
this.passData = Array.prototype.slice(id)
and
this.passData.concat(id)
but it all ends up with
TypeError: this.passData.push/splice/concat is not a function
The question is not that clear, But I understood you are manipulating form data, value of form data returns an Object, Not an array. Objects in JavaScript are represented as key-value pairs, (or attribute-value) pairs.
Example :
var object = {
name : "Jhon",
grade : 12,
gpa : 8.12
}
It is just a collection of key-value pairs, push(), concat() and other methods are supported only for Arrays not for Objects. You can achieve whatever you want simply by creating a new key/attribute and assigning the value to it.
this.passData = this.tribeForm.value
this.passData['tribe_id'] = 1
//or, Objects can also contain nested object
this.passData['someKey'] = {'tribe_id' : 1}
You can create an empty array and push objects to it
Example :
var exampleArray = []
exampleArray.push({'tribe_id' : 1})
Now, it works because exampleArray is an Array not JS object.
Thanks for A2A
First, you need to understand the error:
TypeError: this.passData.push/splice/concat is not a function
Push/splice/concat is functions for Array and because of that the console is yelling at you that the passData is not an Array.
Make sure your passData is an Array and you will able to do so.

Push String into Array in Array at certain position

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.

Resources