如何从json对象数组中获取值并添加到angular中的另一个数组中
我有以下json对象: var-arr=[{ 0:M:“发光二极管” id:1如何从json对象数组中获取值并添加到angular中的另一个数组中,json,angular,typescript,Json,Angular,Typescript,我有以下json对象: var-arr=[{ 0:M:“发光二极管” id:1 mtype:“信息亭套餐” 部分费用:200 tb\U bid\U upins:1 技术投标标志:0 tot_媒体:0 类型:“道路延伸” upin:“AMCADVT1415C0123” upin_id:“2” }, { 1:M:“发光二极管” id:1 mtype:“信息亭套餐” 部分费用:200 tb\U bid\U upins:1 技术投标标志:0 tot_媒体:0 类型:“道路延伸” upin:
mtype:“信息亭套餐”
部分费用:200
tb\U bid\U upins:1
技术投标标志:0
tot_媒体:0
类型:“道路延伸”
upin:“AMCADVT1415C0123”
upin_id:“2”
}, { 1:M:“发光二极管”
id:1
mtype:“信息亭套餐”
部分费用:200
tb\U bid\U upins:1
技术投标标志:0
tot_媒体:0
类型:“道路延伸”
upin:“AMCADVT1415C0123”
upin_id:“2” }] 现在它有两个值,但它可以有多个值,因为它是从数据库获取的。 从这个json i wnat中,使用键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
}]
假设
数据
为数组
,则应在新的空数组中插入数据
const arr=[];
//从原始数组中提取upin、mtype和land
对于(数据项){
arr.push({
upin:item.upin,
mtype:item.mtype,
土地:项目土地
});
}
//或
const arr=data.map((项目)=>{
返回{
upin:item.upin,
mtype:item.mtype,
土地:项目土地
};
});代码>这是纯javascript,与angular无关。您可以使用函数转换数组
const arr2 = this.arr.map(el => ({upins: el.upins, mtyp: el.mtyp, land: el.land}));
第一个答案是正确的。我只想补充一点,对于相同的目标,你们可以使用map方法。这对我来说更舒服:
const newData = data.map(x => {
return {
upin: x.upin,
mtype: x.mtype,
land: x.land
};
});
Map将检查数组中的每个元素,并根据元素属性返回新对象 您可以根据您的需求使用和方法,您可以包括您需要的属性。我在你的数据中没有看到土地属性
const result = arr.map(({ upin, mtype }) => ({
upin, mtype
}));
var arr=[{id:1,
mtype:“信息亭包”,
第十一部分费用:200,
tb_bid_upins:1,
技术投标标志:0,
tot_media:0,
类型:“道路延伸”,
upin:“amcadvt15c0123”,
upin_id:“2”
}, {
id:1,
mtype:“信息亭包”,
第十一部分费用:200,
tb_bid_upins:1,
技术投标标志:0,
tot_media:0,
类型:“道路延伸”,
upin:“amcadvt15c0123”,
upin_id:“2”}]
const result=arr.map({upin,mtype})=>({
upin,mtype
}));
控制台日志(结果)代码>谢谢你的回答。。这也是正确的,但我使用了@sampgun的答案谢谢你的回答。。这也是对的,但我用了@sampgun的答案谢谢你的回答answer@sang很高兴听到这个消息!如果你需要进一步的帮助,请告诉我!