首页 > 解决方案 > 如何从json对象数组中获取值并以角度添加到另一个数组

问题描述

我有以下 json 对象:

var arr = [{ 0:M:“LED” 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”
} , { 1:M: "LED"
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" }]

现在它有两个值,但它可以有多个值,因为它是从数据库中获取的。我想从这个 json 中选择带有 upin、mtype、land 键的值并添加到另一个数组中。

我试过以下

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
}]

标签: jsonangulartypescript

解决方案


假设您应该在一个新的空数组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
  };
});


推荐阅读