Javascript 过滤和操作数组中的对象
我试图在查询后用javascript操作数据,但没有成功 查询为我获取了一个对象数组:Javascript 过滤和操作数组中的对象,javascript,filter,Javascript,Filter,我试图在查询后用javascript操作数据,但没有成功 查询为我获取了一个对象数组: { "keys": [ "n" ], "length": 1, "_fields": [ { "identity": { "low": 10,
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 10,
"high": 0
},
"labels": [
"Lieu"
],
"properties": {
"UpdatedBy": "System",
"Type": "Site / Bâtiment / Etage / Porte / Salle / Lit / Baie / Local",
"CreatedBy": "System",
"Updated": {...},
"Environnement": "Modèle",
"Code": "Bat12",
"Nom": "ModèleLieu",
"Created": {...}
}
}
],
"_fieldLookup": {
"n": 0
}
},
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 11,
"high": 0
},
"labels": [
"Lieudit"
],
"properties": {
"UpdatedBy": "production",
"Type": "Lit / Baie / Local",
"CreatedBy": "other",
"Updated": {...},
"Environnement": "production",
"Code": "Bat13",
"Nom": "lieudit",
"Created": {...}
}
}
],
"_fieldLookup": {
"n": 0
}
},
我想用以下格式创建一个对象数组:
const element = {
id: 10,
Code: "Bat12",
CreatedBy: "System",
Environnement: "Modèle",
Nom: "ModèleLieu",
Type: "Site / Bâtiment / Etage / Porte / Salle / Lit / Baie / Local",
UpdatedBy: "System",
},
{
id: 11,
Code: "Bat13",
CreatedBy: "other",
Environnement: "production",
Nom: "lieudit",
Type: "Lit / Baie / Local",
UpdatedBy: "production",
};
这意味着绕过对象“CreatedBy”、“UpdateBy”,并将identity.low和属性保持在与id相同的级别。
也就是说,属性可能因对象而异,因此我不能使用属性的名称
到目前为止,我已经做到了
var componentArr = [];
result.records.forEach((record) => {
componentArr.push({
id: record._fields[0].identity.low,
name: record._fields[0].properties,
});
});
但是“id”和“properties”不在同一级别,“createdBy”和“update”让我很恼火
请帮助:(应该很简单。请尝试以下操作:
var data = [{
"keys": ["n"],
"length": 1,
"_fields": [
{
"identity": {
"low": 10,
"high": 0
},
"labels": [
"Lieu"
],
"properties": {
"UpdatedBy": "System",
"Type": "Site / Bâtiment / Etage / Porte / Salle / Lit / Baie / Local",
"CreatedBy": "System",
"Updated":"",
"Environnement": "Modèle",
"Code": "Bat12",
"Nom": "ModèleLieu",
"Created":""
}
}
],
"_fieldLookup": {"n": 0}
},
{
"keys": [
"n"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 11,
"high": 0
},
"labels": [
"Lieudit"
],
"properties": {
"UpdatedBy": "production",
"Type": "Lit / Baie / Local",
"CreatedBy": "other",
"Updated": "",
"Environnement": "production",
"Code": "Bat13",
"Nom": "lieudit",
"Created": ""
}
}
],
"_fieldLookup": {
"n": 0
}
}];
const newFormat = data.map(item => item._fields.map(field => {
const {Updated, Created, ...props} = field.properties;
return ({id: field.identity.low, ...props})
})).flat();
console.log(newFormat)
您可以使用来获得所需的结果
const arr=[
{
键:[“n”],
长度:1,
_字段:[
{
身份:{
低位:10,
高:0,,
},
标签:[“代替”],
特性:{
更新人:“系统”,
类型:“Site/B–timent/Etage/Porte/Salle/Lit/Baie/Local”,
创建方式:“系统”,
更新:{},
环境:“莫代尔”,
代码:“Bat12”,
名字:“莫代莱乌”,
创建:{},
},
},
],
_字段查找:{
n:0,
},
},
{
键:[“n”],
长度:1,
_字段:[
{
身份:{
低位:11,
高:0,,
},
标签:[“Lieudit”],
特性:{
更新人:“生产”,
类型:“Lit/Baie/Local”,
创建方式:“其他”,
更新:{},
环境:“生产”,
代码:“Bat13”,
名字:“lieudit”,
创建:{},
},
},
],
_字段查找:{
n:0,
},
},
];
常量结果=arr.map((obj)=>{
常量字段=对象字段[0];
常量id=_fields.identity.low;
常数{
代码,
由,
环境,
笔名,
类型,
更新人:,
}=\u字段属性;
返回{id,Code,CreatedBy,Environnement,Nom,Type,UpdatedBy};
});
console.log(结果);