javascript - 展平一组对象和字符串
问题描述
我有一个这样的对象数组:
const arr = [ {
realEstateBasicInfoResDto: {
type: "Detached house",
kind: "Class01"
},
assetId: "1",
realEstateAddressInfoResDto: {
county: null,
municipality: null,
municipalitySection: null,
address: "Attica, N. Ionia, Iolkou",
addrNumberFrom: 8,
addrNumberTo: 0,
addrLetterFrom: null,
addrLetterTo: null,
addressSec: null,
addrSecLetterFrom: null,
addrSecLetterTo: null,
addrSecNumberFrom: 0,
addrSecNumberTo: 0,
postalCode: "1"
},
realEstateEstimationValuesResDtos: [ ]
},
{
realEstateBasicInfoResDto: {
type: "Detached house",
kind: "Class01"
},
assetId: "11",
realEstateAddressInfoResDto: {
county: null,
municipality: null,
municipalitySection: null,
address: "Attica, N. Ionia, Iolkou",
addrNumberFrom: 8,
addrNumberTo: 0,
addrLetterFrom: null,
addrLetterTo: null,
addressSec: null,
addrSecLetterFrom: null,
addrSecLetterTo: null,
addrSecNumberFrom: 0,
addrSecNumberTo: 0,
postalCode: "2"
},
realEstateEstimationValuesResDtos: [ ]
},
]
我希望输出只有属性,里面没有对象键。
我正在使用这个功能:
const flatten = o =>
Object.keys(o).reduce(
(obj, key) =>
typeof o[key] === "object" && o[key] !== null
? { ...obj, ...flatten(o[key]) }
: { ...obj, [key]: o[key] },
{}
);
它似乎工作正常,但我在我的对象数组上得到的结果是这样的:
flatten(arr) =
{
addrLetterFrom: null
addrLetterTo: null
addrNumberFrom: 8
addrNumberTo: 0
addrSecLetterFrom: null
addrSecLetterTo: null
addrSecNumberFrom: 0
addrSecNumberTo: 0
address: "Attica, N. Ionia, Iolkou"
addressSec: null
assetId: "3"
county: null
kind: "Class01"
municipality: null
municipalitySection: null
postalCode: null
type: "Detached house"
}
它实际上只返回最后一个展平的对象。我想返回一个两个对象都展平的数组。我错过了什么?谢谢。
解决方案
您的flatten
函数处理单个对象,因此您需要使用 迭代数组Array.map()
,并将您的 flatten 函数应用于每个对象:
const arr = [{"realEstateBasicInfoResDto":{"type":"Detached house","kind":"Class01"},"assetId":"1","realEstateAddressInfoResDto":{"county":null,"municipality":null,"municipalitySection":null,"address":"Attica, N. Ionia, Iolkou","addrNumberFrom":8,"addrNumberTo":0,"addrLetterFrom":null,"addrLetterTo":null,"addressSec":null,"addrSecLetterFrom":null,"addrSecLetterTo":null,"addrSecNumberFrom":0,"addrSecNumberTo":0,"postalCode":"1"},"realEstateEstimationValuesResDtos":[]},{"realEstateBasicInfoResDto":{"type":"Detached house","kind":"Class01"},"assetId":"11","realEstateAddressInfoResDto":{"county":null,"municipality":null,"municipalitySection":null,"address":"Attica, N. Ionia, Iolkou","addrNumberFrom":8,"addrNumberTo":0,"addrLetterFrom":null,"addrLetterTo":null,"addressSec":null,"addrSecLetterFrom":null,"addrSecLetterTo":null,"addrSecNumberFrom":0,"addrSecNumberTo":0,"postalCode":"2"},"realEstateEstimationValuesResDtos":[]}];
const flatten = o =>
Object.keys(o).reduce(
(obj, key) =>
typeof o[key] === "object" && o[key] !== null ?
{ ...obj,
...flatten(o[key])
} :
{ ...obj,
[key]: o[key]
}, {}
);
const result = arr.map(flatten);
console.log(result);
推荐阅读
- java - 创建 Apache POI 实例时代码停止工作且没有任何错误
- android - Android:当我们点击推送通知时,如何在登录活动/身份验证后打开活动
- c++ - 函数模板中的数组值类型推导
- robotframework - 使用 TypeError 运行 robotsframework 远程库关键字:run_keyword() 恰好需要 4 个参数(给定 3 个)
- reactjs - 反应:从另一个组件调用 setState
- spring - Spring AOP:仅从另一个方法调用的捕获方法
- angularjs - 使用 angularjs 在 MVC .net 应用程序中维护会话
- c# - 从.net core 2.1中的单例服务注入范围服务
- javascript - 我可以让一个属性在 vue v-for 中只出现一次吗
- spring-boot - 如何在 Docker 容器中提供 libgthread-2.0.so.0