5

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

2 Answers 2

13

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
Sign up to request clarification or add additional context in comments.

Comments

12

Thing you are trying to achieve is to create object, not an array.

You can do it:

let pusheditems = {};
pusheditems[this.yesvalue] = this.selectedtruck;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.