json - 如何从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
}]
解决方案
假设您应该在一个新的空数组data
中array
插入数据。
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
};
});
推荐阅读
- java - 如何将函数重构为目标函数?
- html - 如何像输入字段一样设置文本区域的样式
- java - 使用 php mysqli 获取数组到 java
- python - 如何在熊猫中将一段时间内具有不同(但有时重叠)索引的数据帧组合在一起?
- python - Geopandas - 基于数据框中人口的颜色
- c# - “'Form1' 已经定义了一个名为 '.ctor' 的成员,具有相同的参数类型”
- excel - Laravel Excel 上传和进度条
- c# - 切换当前显示的表单(winforms)
- asp.net - 由于 SemaphoreFullException,无法登录我的 Web 应用程序
- javascript - 重定向到 json 响应中的 URL