How to Push Array in Array in Angular

how to push array in array in angular?

The length of List will return 2 and the index of an array starts from 0 so the result.length - 1.

So you can do like this:

Change:

var i = 0; // Start from zero because arrays are starts from `0` so we can access the first item as list[0]

And

result.length - 1; // Subtract 1 from length of list

Code after applying the change:

dialogRef.afterClosed().subscribe(result => {
if(result != '' && result != undefined && result != null){
for(var i = 0; i <= result.length - 1; i++){
this.imagesArray.push(result[i]);
}
}
});

You can do this without subtracting the -1 from the length, just change the condition parameter to i < result.length as:

dialogRef.afterClosed().subscribe(result => {
if(result != '' && result != undefined && result != null){
for(var i = 0; i < result.length; i++){
this.imagesArray.push(result[i]);
}
}
});

StackBlitz Example

How to push object into an array? in Angular 7

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

Angular Typescript array only adds last element on array.push()

When you modify Class directly, like here

   tempClass.timei+1;
tempClass.id=i;

You are modifying the existing class instance.

Because you add this already existing instance in array with

arrayTemp.push(tempClass);

It means you edit the class you created in the beginning with

let tempClass ={} as ABC;

What you actually want to do, is create a new class instance for each iteration, like so

addToArray(){
let arrayTemp : ABC[]= [];

for (let i = 0; i < 4; i++) {
// create new instance of class for each iteration
let tempClass = new ABC();
tempClass.timei+1;
tempClass.id=i;

let val=i;
arrayTemp.push(tempClass);
console.log(arrayTemp[val]); // here class objects displayed correctly
}

console.log(arrayTemp); // here all elements are just the last added class object

}
}

Edit:

Don't forget to create your class with new keyword! If you do intialization directly as let tempClass ={} as ABC; you wont get actual representation of a class, you only get empty object that only looks like a ABC class to typescript.

Add items in array angular 4

Yes there is a way to do it.

First declare a class.

//anyfile.ts
export class Custom
{
name: string,
empoloyeeID: number
}

Then in your component import the class

import {Custom} from '../path/to/anyfile.ts'
.....
export class FormComponent implements OnInit {
name: string;
empoloyeeID : number;
empList: Array<Custom> = [];
constructor() {

}

ngOnInit() {
}
onEmpCreate(){
//console.log(this.name,this.empoloyeeID);
let customObj = new Custom();
customObj.name = "something";
customObj.employeeId = 12;
this.empList.push(customObj);
this.name ="";
this.empoloyeeID = 0;
}
}

Another way would be to interfaces read the documentation once - https://www.typescriptlang.org/docs/handbook/interfaces.html

Also checkout this question, it is very interesting - When to use Interface and Model in TypeScript / Angular2

Push Item From One Array Into another Array in Angular

$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.tasksDone = [];
$scope.addToTasksDone = function(index) {// IM FAILNG HERE};
$scope.tasksDone.push($scope.tasks[index]);
}

How to push object into array angular 8

Please try the below code.

deleteAllContextData(data) {
const newSelectedParameterContext = {
'records': []
};
this.selectedParameterContext.records.forEach(function (record) {
const newSelectedParameterValues = [];
record.values.forEach(function (parameter, parameterIndex) {
const isValid = data.data.values.find(function (item) {
return parameter.value === item.value && parameter.label === item.label;
});
if (isValid) {
newSelectedParameterValues.push(parameter);
}
});
newSelectedParameterContext.records.push({ ...record, 'values': newSelectedParameterValues });
});
this.selectedParameterContext = newSelectedParameterContext;

}

How to filter array and push it to another array in Angular

Use filter() function as follows:

arrangeEvents(){
let currentDateObj = new Date();
this.upcomingEvents = this.eventList.filter(event => new Date(event.date) > currentDateObj);
this.pastEvents = this.eventList.filter(event => new Date(event.date) <= currentDateObj);
}


Related Topics



Leave a reply



Submit