Angular2,Typescript:How to push array of objects into another array of objects with only a few fields of the object Angular2,Typescript:How to push array of objects into another array of objects with only a few fields of the object angular angular

Angular2,Typescript:How to push array of objects into another array of objects with only a few fields of the object


You're looking for the array map() method:

const newArray = array.map(o => {  return { name: o.name, courseid: o.courseid };});


Try this:

let data = [{    "name": "Btech",    "courseid": "1",    "courserating": 5,    "points": "100",    "type": "computers"},{    "name": "BCom",    "courseid": "2",    "courserating": 5,    "points": "100",    "type": "computers"}];let other = []; // your other array...data.map(item => {    return {        courseid: item.courseid,        name: item.name    }}).forEach(item => other.push(item));console.log(JSON.stringify(other))// => [{"courseid":"1","name":"Btech"},{"courseid":"2","name":"BCom"}]


You can simply do it like this.

//assign your array of object to variablevar youArray:Array<any>= [{    "name": "Btech",    "courseid": "1",    "courserating": 5,    "points": "100",    "type": "computers"},{    "name": "BCom",    "courseid": "2",    "courserating": 5,    "points": "100",    "type": "computers"}];var resultArray:Array<any>=[] //empty array which we are going to push our selected items, always define types youArray.forEach(i=>{    resultArray.push(   {    "name":i.name,    "courseid":i.courseid   });});console.log(resultArray)

if you still have doubts about this.please follow this url