展平一组对象和字符串

时间:2019-03-29 16:39:38

标签: javascript arrays reactjs

我有一个这样的对象数组:

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"    
}

它实际上仅返回最后展平的对象。我想返回两个对象都展平的数组。我想念什么?谢谢。

1 个答案:

答案 0 :(得分:0)

您的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);