How to add element to empty array - arrays

I have a button, when I press it will run the program below, but the array always starts from index 0 continuously
_check(){
let name = this.shadowRoot.querySelector('.name').value
var arr =[]
arr.push(name)
console.log(arr);
}
i expect the output ["text","text",...] but the actual output is ["text"]

As CinCout mentioned - the issue is that of scope - specifically you are resetting arr back to an empty within the function scope and then pushing the single value into it - meaning it will never have an more that the single value in it.
The solution is to set the array outside the scope of the function and then you will be able to push the value into the array.
let arr =[];
function _check(){
let name = document.querySelector('.name').value;
arr.push(name);
console.log(arr);
}
<button type="button" onclick="_check()" class="name" value="test">Click me</button>

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)

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.

Actionscript 3: each line from text file as a element in array

So my problem is that trace within the function does trace the first element of the array, but the trace outside if the function does not. I do declare the array variable outside the function, but the data wont save to the array variabel.
var oppgaveLoader:URLLoader = new URLLoader();
oppgaveLoader.load(new URLRequest("oppgaver.txt"));
var oppgaveNr = 0
//store line of text on an array called oppgaver
var oppgaver:Array = []
var oppg:Array = new Array()
oppgaveLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event){
oppgaver = e.target.data.split(/\n/)
trace(oppgaver[0]) //This one traces the frist item in the array
}
trace(oppgaver[0])//This one does not trace the first one in the array
Does anyone know why and/or how to fix it if posible?
The file "oppgaver.txt" is located in the same directory as my .fla file
The file "oppgaver.txt" is laid out like this (the text is in norwegian, but each line is going to be a item in the array):
Hvor gjelder forbudsskilt hvis ikke annet er oppgitt?
Hvordan foretar du best mulig bremsing og unnastyring?
Hvordan bør du normalt plassere bilen på en vanlig 2-felst vei?
It's a synchronicity problem.
the last trace happens immediately after you set up your arrays, but those arrays are still empty.
only when the onLoaded function gets called, asynchronously by the URLLoader, they get populated and you can trace their values.
that event listener basically lets you react to an event that happens in the future at some point.

why array become blank after splitting into multiple small array?

hello I have one a array $scope.name .I am spliting the array into small arrays .But after spliting the array .it become blank why ?
actually I assigned the given array into temp variable and splite the temp variable .Again my $scope.name become blank why ?
here is my plunker
http://plnkr.co/edit/iUscrw0xclHSnsIWMMTM
console.log("before");
console.log($scope.name);
var test=$scope.name;
console.log("after");
console.log($scope.name);
console.log("test");
console.log(test);
var arrays = [], size = 3;
while (test.length > 0)
arrays.push(test.splice(0, size));
console.log(arrays);
console.log("name");
console.log($scope.name);
You are directly assigning object to other object, so that will cause change in any of the object will update other object value.
Use angular.copy instead of assigning object directly, that will create a new clone copy of that object will returned.
var test=angular.copy($scope.name);
Forked Plunkr

AngularJS: ng-repeat an array with keys

I have an array with unordered keys, and I want to display them. The problem is that angular repeats it for all the keys, even when they are not set.
this is the code:
<div ng-controller="MyCtrl">
Hello, {{a[10]}}!
<p ng-repeat="b in a">
1. {{b}}
</p>
</div>
<script>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.a = [];
$scope.a[10] = "aaa";
}
</script>
and this is the output:
Hello, aaa!
1.
1.
1.
1.
1.
1.
1.
1.
1.
1.
1. aaa
i want only the array keys that are set to output. no empty b's please...
here is a jsfiddle
In essence your problem is not AngularJS related but rather how JavaScript works.
If you have an empty array and you assign an element to position 10, then JavaScript will automatically resize the array to 11 elements (since an array index starts at zero) so the index is large enough to hold your element.
You could write extra code to filter the empty elements, but based on what you write, I think you would be better off with using an object to store your data.
I have created a plnkr for your convenience: http://plnkr.co/edit/apRLuJr4zqS2zbMz322Q?p=preview
// Array
$scope.sampleArray = [];
$scope.sampleArray[10] = 'test';
// Object
$scope.sampleObject = {};
$scope.sampleObject[10] = 'test';
As you can see the syntax is very similar, but the output is completely different.
By using an object, you will automatically eliminate the empty lines.
It will also keep your code simpler since you won't have to deal with the empty array elements.
Hope that helps!
There's plenty of ways to do a cleanup on your array inside the controller (e.g. using $watchcallback on a that would remove the empty elements from it whenever it changes).
Here's a solution that uses a simple custom filter, defined in a controller:
function MyCtrl($scope) {
$scope.namea = 'Superhero';
$scope.a = [];
$scope.a[10] = "aaa";
$scope.myFilter = function(item){
return item;
}
}
<p ng-repeat="b in a | filter:myFilter">
1. {{b}}
</p>
As stated in filter docs, the 'filter' filter can take a function:
function: A predicate function can be used to write arbitrary filters.
The function is called for each element of array. The final result is
an array of those elements that the predicate returned true for.

Resources