0

I'm trying to implement a function to select multiple items from one and add/remove to another using typescript.

        //data of the display list, each element has same value for text & value.
        private listA: any = {};  
        private listB: any = {};  

        //string array contains the list of name selected
        private selectA: string[] = [];  
        private selectB: string[] = [];

        private addClick: void {

            //remove all from listA that match name found in selectA
            //add to listB all name found in selectA
        }

How would I go about to implement this function? I already manage to get the data (listA & listB) to populate my list so that is not an issue, my issue is to prepare the data properly for it.

I'm new to typescript & javascript so the syntax is what giving problem now. I tried to work with Object.assign(), delete and couple of other functions I found but no luck so far.

enter image description here

3
  • 2
    Can you update the example with some data in the lists so we can get a better picture of what's going on. listA and listB are arrays? They're typed as objects right now. Commented Jul 3, 2019 at 18:02
  • The listA & listB are list of object that was parsed from a http post response. to be more percise, it contains two string properties calls text & value. listA = response.data.map((val: any) => { const el: any = {}; el.text = val.name; el.value = val.name; return el; }); Commented Jul 3, 2019 at 18:16
  • FYI - You can make your original assignment of listA more concise if you want. this.listA = response.data.map(val => ({ text: val.name, value: val.name })); Commented Jul 3, 2019 at 18:25

3 Answers 3

1

Is this what you're looking for?

  // data of the display list, each element has same value for text & value.
  private listA: any[] = [];
  private listB: any[] = [];

  // string array contains the list of name selected
  private selectA: string[] = [];
  private selectB: string[] = [];

  private addClick(): void {
    // Get items from listA that match selectA.
    const matching = this.listA.filter(x => this.selectA.includes(x.value));

    // Filter listA to only include items that are not a match from selectA.
    this.listA = this.listA.filter(x => !this.selectA.includes(x.value));

    // Add matching items from listA to listB.
    this.listB.push(matching);
  }

this.listB.push(matching) appends the elements to the end of the array. You can also use this.listB.unshift(matching) to prepend them to the start.

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

1 Comment

Thanks t8tortolover, this is exactly what I need, I'll try it out with the unshift as well ...
1

I'm not sure that I understand 100% your problem but I tried this and it seems to work check it and we can modify it till reach what you need

class Test {     //data of the display list, each element has same value for text & value.
    private listA: any = {a:'a','b':'b'};
    private listB: any = {};

//string array contains the list of name selected
    private selectA: string[] = ['a','b'];
    private selectB: string[] = [];

    public addClick(): void {
        this.selectA.forEach((item) => {
            if (this.listA[item]) {
                this.listB[item] = item;
                delete this.listA[item];
            }
        })
    }

    public showResult(){
        console.log('ListA',this.listA)
        console.log('SelectA',this.selectA)
        console.log('ListB',this.listB)
    }

    //remove all from listA that match name found in selectA
    //add to listB all name found in selectA
}

Then we can run this

let test = new Test();

test.showResult();
test.addClick();
test.showResult();

the Result will be as expected

ListA { a: 'a', b: 'b' }
SelectA [ 'a', 'b' ]
ListB {}
ListA {}
SelectA [ 'a', 'b' ]
ListB { a: 'a', b: 'b' }

Comments

0

Steps

  1. Iterate through the selected items from selectA
  2. Get the index of each item in listA
  3. Use splice(param1, param2) method to delete one item at a time from the list
  4. Add the item in selectB list as well as listB

Note:- splice() method will mutate the original list ; If you don't want to mutate the list, you can use filter() method

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.