0

I have an array EventsList. But it doesn't have id column. So I need to push value with key into List1 and List2 from EventsList. Can we do with the below code.

this.GetEventsList().then(response=>{
        if(response.data){
            _self.EventsList=response.data;
            _self.EventsList.forEach (function(events){
                if(events.category == "a")
                    _self.List1.push(events);
                else
                    _self.List2.push(events);
            });
        }
    });
2
  • Do the events have any unique value? Otherwise I think you will have to generate your own key. Commented Mar 26, 2020 at 13:39
  • No. That's why I trying to add ID as a unique value Commented Mar 26, 2020 at 13:40

1 Answer 1

2

You could just have a count be the id and push it as a object into the list. You could also use the length of the current list as the id if you are going to continuously add items to the same list.

this.GetEventsList().then(response=>{
    if(response.data){
        _self.EventsList=response.data;
        var count = 0;
        _self.EventsList.forEach (function(events){
            if(events.category == "a")
                _self.List1.push({id:count,events:events});
            else
                _self.List1.push({id:count,events:events});

            count++;
        });
    }
});
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.