I would like to push an array to another array but the result generates an incorrect result
let pusheditems:any[] = [];
pusheditems.push(this.yesvalue);
pusheditems.push(this.selectedtruck);
Later when i console.log(pusheditems)
Am getting an array of type
array(0->yes array, 1->select truck array)
What am looking for is to change the index values of 0,1 to strings like yes, truck
So i would expect to get
array(yes->yes array, truck->select truck array)
I have also tried
pusheditems.push({yes:this.yesvalue}); //adding yes
pusheditems.push({truck:this.selectedtruck}); //adding truck
But this doesnt work
The values of
this.yesvalues and this.selectedtruck are also arrays
What do i need to add further
Use reduce() to Push Key-Value Pair Into an Array in JavaScript. The reducer function got executed by the reduce() method. It returns only one value, and that is the accumulated answer of the function.
To push an object to an array: Set the type of the array to Type[] . Set the type of the object to Type . Use the push() method to push the object to the array.
To push an object into an array, call the push() method, passing it the object as a parameter. For example, arr. push({name: 'Tom'}) pushes the object into the array. The push method adds one or more elements to the end of the array.
In Typescript, arrays can have keys only of type number. You need to use Dictionary
object, like:
let pusheditems: { [id: string]: any; } = {}; // dictionary with key of string, and values of type any
pusheditems[this.yesvalue] = this.selectedtruck; // add item to dictionary
Thing you are trying to achieve is to create object, not an array.
You can do it:
let pusheditems = {};
pusheditems[this.yesvalue] = this.selectedtruck;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With