简体   繁体   English

Angular2,Typescript:如何将对象数组推入另一个对象数组,只有对象的几个字段

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

I know how to operate on array of objects but never had the necessity to push one array data into another. 我知道如何操作对象数组,但从来没有必要将一个数组数据推送到另一个数组。 I need to push an array of objects into another array with only 2 fields of the object. 我需要将一个对象数组推入另一个只有2个对象字段的数组中。 Right now my object format is somewhat like this 现在我的对象格式有点像这样

data: [{
        "name": "Btech",
        "courseid": "1",
        "courserating": 5,
        "points": "100",
        "type": "computers"
    },
   {
        "name": "BCom",
        "courseid": "2",
        "courserating": 5,
        "points": "100",
        "type": "computers"
    }];

I want to push this into another array but I want only courseid and name in the object. 我想把它推入另一个数组,但我只想在对象中使用courseid和name。 I've read that we need to initialise the object in the constructor, use slice() and a few other functions and then push but I don't know how can I do it for mine since I need to push one array data into another.Kindly someone help me in this regard. 我已经读过我们需要在构造函数中初始化对象,使用slice()和其他一些函数然后推送但是我不知道我怎么能为我做这个因为我需要将一个数组数据推送到另一个在这方面,有人帮助我。

You're looking for the array map() method : 您正在寻找数组map()方法

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 variable

var 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 如果你仍然对此有疑问。请关注此网址

Map to a returning JSON data, subscribe it to an existing array or an empty one. 映射到返回的JSON数据,将其订阅到现有数组或空数组。 #typescript #typescript

  let pictimeline= []; 
    var timeline = this.picService.fetchtimeline(this.limit)


.map((data : Response) => data.json())
    .subscribe(pictimeline=> this.pictimeline = pictimeline);


console.log(this.pictimeline);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM