0

I have service that gets me array of types:

ngOnInit() {
    this.coreService.getByType( this.name ).subscribe(
            response => { this.handleSuccess( response ); },
            error => { console.error( error ); });

}

handleSuccess( coreTypes ) {
    var data = [];
    var pushedItems = [];
    coreTypes.forEach( ( coreType ) => {
        var entriesForType = [];
        entriesForType.push( coreType );    
            if ( entriesForType.length > 0 ) {
                entriesForType.forEach( entry => this.data.push( entry ) );
                this.data = data;
            if (data.length > 0) {
                data.forEach( d =>  this.item.value = d && pushedItems.push(this.item));
            }
                if(this.gridOptions.api !== null){
                    this.gridOptions.api.setRowData( this.pushedItems );
                }
            } 
    });
}

Currently, this.data is creating me array like this this.data = ["one","two","three"]

What i need is to create array of object that will look like this

pushedItems = [{value:"one"},{value:"two"},{value:"three"}];

I defined item: Object; and in contstructor this.item = {value:""};

But in function, when i set this.item.value = d ... it keeps showing me error "Property 'value' does not exist on type 'Object' ...Any help to achieve array like pushedItems?

1
  • Use this in subscriber method like this subscribe( (response:any)=> { this.handleSuccess( response ); }); Commented Jun 27, 2018 at 14:14

2 Answers 2

2
handleSuccess() {
  const p = [];
  this.coreTypes.forEach(coretype => {
   let obj = {};
   obj[coretype] = coretype;
   p.push(obj);
  });
}

handleSuccess() {
  const p = [];
  this.coreTypes.forEach(coretype => {      
   p.push({coretypes:coretypes});
  });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Code only answer can't be good answers. Could you explain a little ?
Second example is working just fine ...you can just type p.push({value: coretype});
1

Instead of pushing your object as:

arr.push(this.object);

you should just push it like:

arr.push({
  value: this.object,
})

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.