简体   繁体   English

如何从json对象数组中获取值并以角度添加到另一个数组

[英]How to get values from json object array and add to another array in angular

I have Following json object :我有以下 json 对象:

var arr =[{ 0:M: "LED" id: 1 var arr =[{ 0:M: "LED" id: 1
mtype: "KIOSK PACKAGE" mtype: "自助服务终端包"
part_fees: 200部分费用:200
tb_bid_upins: 1 tb_bid_upins: 1
tech_bid_flag: 0 tech_bid_flag: 0
tot_media: 0 tot_media: 0
type: "Road Stretch"类型:“道路伸展”
upin: "AMCADVT1415C0123" upin: "AMCADVT1415C0123"
upin_id: "2" upin_id: "2"
}, { 1:M: "LED" }, { 1:M: "LED"
id: 1编号:1
mtype: "KIOSK PACKAGE" mtype: "自助服务终端包"
part_fees: 200部分费用:200
tb_bid_upins: 1 tb_bid_upins: 1
tech_bid_flag: 0 tech_bid_flag: 0
tot_media: 0 tot_media: 0
type: "Road Stretch"类型:“道路伸展”
upin: "AMCADVT1415C0123" upin: "AMCADVT1415C0123"
upin_id: "2" }] upin_id: "2" }]

Now it has two values,but it can have multiple value because it is fetch from database.现在它有两个值,但它可以有多个值,因为它是从数据库中获取的。 From this json i wnat to pick values with key upin,mtype,land and add to another array.从这个 json 我wnat 选择带有键 upin、mtype、land 的值并添加到另一个数组中。

I have tried following我试过跟随

for(let item of data){
    // this.console.log(item)
     this.upins = item.upin;
     this.console.log(this.upins);
    }
this.console.log(this.upins);```

It shows last index value

I want result as follows

var arr = [{
upins: abc,
mtyp:xyz,
land:123
},{
upins:123,
mtype:pqr,
land:555
}]

Assuming data as array you should insert data in a new empty array.假设dataarray您应该在新的空数组中插入数据。

 const arr = []; // extract upin, mtype, land from the original array for (let item of data) { arr.push({ upin: item.upin, mtype: item.mtype, land: item.land }); } // OR const arr = data.map((item) => { return { upin: item.upin, mtype: item.mtype, land: item.land }; });

You can use map and destructing method for your requirement, you can include what properties you need.您可以根据您的要求使用mapdestructing方法,您可以包含您需要的属性。 I did not see land property in your data.我没有在你的数据中看到土地财产。

const result = arr.map(({ upin, mtype }) => ({
  upin, mtype
}));

 var arr =[{ id: 1, mtype: "KIOSK PACKAGE", part_fees: 200, tb_bid_upins: 1, tech_bid_flag: 0, tot_media: 0, type: "Road Stretch", upin: "AMCADVT1415C0123", upin_id: "2" }, { id: 1, mtype: "KIOSK PACKAGE", part_fees: 200, tb_bid_upins: 1, tech_bid_flag: 0, tot_media: 0, type: "Road Stretch", upin: "AMCADVT1415C0123", upin_id: "2" }] const result = arr.map(({ upin, mtype }) => ({ upin, mtype })); console.log(result);

this is pure javascript and it has nothing to do with angular.这是纯javascript,与角度无关。 you can use map function to transform arrays.您可以使用map函数来转换数组。

const arr2 = this.arr.map(el => ({upins: el.upins, mtyp: el.mtyp, land: el.land}));

The first answer is right.第一个答案是对的。 I only want to add that you can use map method for same goal.我只想补充一点,您可以将 map 方法用于同一目标。 It's more comfortable for me:对我来说更舒服:

const newData = data.map(x => {
    return {
        upin: x.upin,
        mtype: x.mtype,
        land: x.land
    };
});

Map will check each element in the array and return new object based on element properties. Map 将检查数组中的每个元素并根据元素属性返回新对象。

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

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