0

I have method which take interface parameter. But i want that my original parameter "selectItemList" not change. So, I create new constant "itemlist". And I added item to "itemList".But, when I added item to itemList, then automatically added to selectItemList. I want only change "itemList" not " selectItemList". Where is my mistake?

SelectItem.ts (Interface)

export interface SelectItem {
    label?: string;
    value: any;
    styleClass?: string;
    icon?: string;
    title?: string;
    disabled?: boolean;
}

My method

  addUnselectedItem(selectItemList: SelectItem[]): SelectItem[] {
    const itemList = selectItemList;
    itemList.unshift({ value: "", label: "Please Select" });
    return itemList;
  }
3
  • 1
    i hope this will help you. stackblitz.com/edit/angular-gp1t3t?file=src/app/… Commented Oct 8, 2018 at 12:26
  • Thanks @shubhamsingh. This answer also worked like Sarthak Aggarwal's answer. Commented Oct 8, 2018 at 12:33
  • hmm i know hence i m also upvote him . Commented Oct 8, 2018 at 12:44

1 Answer 1

3

You are making reference of 'selectItemList' in 'itemList'. You need to create new array to solve the above problem.

There are 2 methods to do the same.

  1. copy all the elements in new array and then modify new array

  2. use 'slice' method(returns a shallow copy of a portion of an array) of Array.

    const itemList = selectItemList.slice(0,selectItemList.length);

Sign up to request clarification or add additional context in comments.

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.