假设我有对象数组,我需要使用之前的数据创建新的一个对象数组。在我的情况下,我需要过滤所有数据。 ES6的最佳解决方案是什么?
data = [{
deviceId:1
deviceStatus:"Offline"
deviceType:"Tag"
isConfigured:true
isEnabled:true
isLocalized:false
isMasterAssigned:false
lastAliveMessage:null
location:{roomId: 5, name: "Room_207", zones: null, plan: null}
name:"Tag For sending an alarm"
},{
deviceId:2
deviceStatus:"Online"
deviceType:"Tag"
isConfigured:true
isEnabled:true
isLocalized:false
isMasterAssigned:false
lastAliveMessage:null
location:{roomId: 6, name: "Room_208", zones: null, plan: null}
name:"Some Text"
}]
我只需要过滤所需的数据。
filteredData=[{
deviceId:2
deviceStatus:"Online"
deviceType:"Tag"
lastAliveMessage:null
name: 'name:"Some Text'
location: location.name
},{
deviceId:2
deviceStatus:"Online"
deviceType:"Tag"
lastAliveMessage:null
name: 'name:"Some Text'
location: location.name
}]
答案 0 :(得分:4)
使用Array#map函数迭代它们并创建另一种类型的对象。
const data = [{
deviceId:1,
deviceStatus:"Offline",
deviceType:"Tag",
isConfigured:true,
isEnabled:true,
isLocalized:false,
isMasterAssigned:false,
lastAliveMessage:null,
location:{roomId: 5, name: "Room_207", zones: null, plan: null},
name:"Tag For sending an alarm"
},{
deviceId:2,
deviceStatus:"Online",
deviceType:"Tag",
isConfigured:true,
isEnabled:true,
isLocalized:false,
isMasterAssigned:false,
lastAliveMessage:null,
location:{roomId: 6, name: "Room_208", zones: null, plan: null},
name:"Some Text"
}];
const newObj = data.map(item => ({
deviceId: item.deviceId,
deviceStatus: item.deviceStatus,
deviceType: item.deviceType,
lastAliveMessage: item.lastAliveMessage,
name: item.name,
location: item.location.name
}));
console.log(newObj);

答案 1 :(得分:0)
您可以为所需的密钥设置一个白名单。
> java.exe -jar myjar.jar MyClass """*.*"""