我有一个数组如下,
data = [
{
"is_new":1,
"is_delayed":24,
"is_active":25,
"name":"london"
},
{
"is_new":1,
"is_delayed":90,
"is_active":90,
"name":"paris"
},
{
"is_new":1,
"is_delayed":45,
"is_active":45,
"name":"India"
}
]
我想将数组中的对象转换为普通对象,其中"is_delayed">和"is_action">值分别为0和1。
所以我最终期望的对象看起来像这样:
myObj = {
"is_new":1,
"is_delayed":0,
"is_active":1,
"name":"london"
},
{
"is_new":1,
"is_delayed":0,
"is_active":1,
"name":"paris"
},
{
"is_new":1,
"is_delayed":0,
"is_active":1,
"name":"India"
}
我该怎么做呢?
您可以使用Array#map
创建一个数组,其中包含具有更新属性的新对象。
let data=[{is_new:1,is_delayed:24,is_active:25,name:"london"},{is_new:1,is_delayed:90,is_active:90,name:"paris"},{is_new:1,is_delayed:45,is_active:45,name:"India"}];
let res = data.map(o => ({...o, is_delayed: 0, is_active: 1}));
console.log(res);