首页 > 解决方案 > 如何匹配值然后为对象添加值?

问题描述

当前匹配来自两个不同对象的值。找到匹配项后,将一个对象的值添加到另一个对象。

递归执行此操作并返回一个新对象。

我有点失落。我已经在 .map 中尝试过 .map 并且我已经尝试过过滤器。

我将在下面发布代码,任何帮助将不胜感激。这是我最后的障碍。

我想匹配locationpostcode 匹配年份。然后添加price到嵌套对象year

这是我当前的功能:

    findLocationYearAddPrice = () => {
        // console.log('compiledData', compiledData.map(i => i.location))
        // console.log('data', data.map(i => i.location))
        compiledData.map(i => {
            data.map(j => {
                if (i.location === j.location) {
                    Object.keys(i.year).filter(k => {
                        if (k === j.year) {
console.log('k', k)
                        }
                        return '';
                        //  console.log('undefined this', k)
                    });
                }
            });
        });

这是两组(简要)数据。第一组是我想要匹配年份的位置,然后提取价格并将其添加到第二个数据对象的格式更好、更简洁的 JSON 中。

下面是data函数中提到的

const data = [
    {
        "location": "City of London",
        "postcode": "EC1A 7BE",
        "price": "91449",
        "year": "95"
    },
    {
        "location": "Barking & Dagenham",
        "postcode": "RM9 4TP",
        "price": "50460",
        "year": "95"
    },
    {
        "location": "Barnet",
        "postcode": "EN5 5RP",
        "price": "93285",
        "year": "95"
    },
    {
        "location": "Bexley",
        "postcode": "DA16 3DN",
        "price": "64958",
        "year": "95"
    },
    {
        "location": "Brent",
        "postcode": "HA9 9AA",
        "price": "71307",
        "year": "95"
    },
    {
        "location": "Bromley",
        "postcode": "BR1 3NN",
        "price": "81671",
        "year": "95"
    },
    {
        "location": "Camden",
        "postcode": "NW1 8QL",
        "price": "120933",
        "year": "95"
    },
    {
        "location": "Croydon",
        "postcode": "CR2 7PA",
        "price": "69158",
        "year": "95"
    },
    {
        "location": "Ealing",
        "postcode": "W13 0JX",
        "price": "79886",
        "year": "95"
    },
    {
        "location": "Enfield",
        "postcode": "EN2 7AA",
        "price": "72515",
        "year": "95"
    },
    {
        "location": "Greenwich",
        "postcode": "SE9 1DU",
        "price": "62300",
        "year": "95"
    },
    {
        "location": "Hackney",
        "postcode": "E8 1FD",
        "price": "61297",
        "year": "95"
    },
    {
        "location": "Hammersmith & Fulham",
        "postcode": "W6 8AB",
        "price": "124903",
        "year": "95"
    },
    {
        "location": "Haringey",
        "postcode": "N8 9DJ",
        "price": "76288",
        "year": "95"
    },
    {
        "location": "Harrow",
        "postcode": "HA3 5AG",
        "price": "84770",
        "year": "95"
    },
    {
        "location": "Havering",
        "postcode": "RM12 6PL",
        "price": "68000",
        "year": "95"
    },
    {
        "location": "Hillingdon",
        "postcode": "UB8 1JZ",
        "price": "73835",
        "year": "95"
    },
    {
        "location": "Hounslow",
        "postcode": "TW3 2DL",
        "price": "72232",
        "year": "95"
    },
    {
        "location": "Islington",
        "postcode": "N5 1RA",
        "price": "92516",
        "year": "95"
    },
    {
        "location": "Kensington & Chelsea",
        "postcode": "W8 5SA",
        "price": "182695",
        "year": "95"
    },
    {
        "location": "Kingston upon Thames",
        "postcode": "KT5 9NX",
        "price": "80876",
        "year": "95"
    },
    {
        "location": "Lambeth",
        "postcode": "SW9 8HE",
        "price": "67771",
        "year": "95"
    },
    {
        "location": "Lewisham",
        "postcode": "SE6 4RE",
        "price": "60491",
        "year": "95"
    },
    {
        "location": "Merton",
        "postcode": "SW20 9JT",
        "price": "82071",
        "year": "95"
    },
    {
        "location": "Newham",
        "postcode": "E13 9AP",
        "price": "53539",
        "year": "95"
    },
    {
        "location": "Redbridge",
        "postcode": "IG6 1NA",
        "price": "72190",
        "year": "95"
    },
    {
        "location": "Richmond upon Thames",
        "postcode": "TW9 1EZ",
        "price": "109326",
        "year": "95"
    },
    {
        "location": "Southwark",
        "postcode": "SE1 5AA",
        "price": "67885",
        "year": "95"
    },
    {
        "location": "Sutton",
        "postcode": "SM1 1JA",
        "price": "71537",
        "year": "95"
    },
    {
        "location": "Tower Hamlets",
        "postcode": "E14 7JE",
        "price": "59865",
        "year": "95"
    },
    {
        "location": "Waltham Forest",
        "postcode": "E17 7LP",
        "price": "61319",
        "year": "95"
    },
    {
        "location": "Wandsworth",
        "postcode": "SW12 8PB",
        "price": "88559",
        "year": "95"
    },
    {
        "location": "Westminster",
        "postcode": "SW1H 0BB",
        "price": "133025,",
        "year": "95"
    },
    {
        "location": "City of London",
        "postcode": "EC1A 7BE",
        "price": "108999",
        "year": "96"
    },
    {
        "location": "Barking & Dagenham",
        "postcode": "RM9 4TP",
        "price": "50828",
        "year": "96"
    },
    {
        "location": "Barnet",
        "postcode": "EN5 5RP",
        "price": "91111",
        "year": "96"
    },
    {
        "location": "Bexley",
        "postcode": "DA16 3DN",
        "price": "63996",
        "year": "96"
    },
    {
        "location": "Brent",
        "postcode": "HA9 9AA",
        "price": "72806",
        "year": "96"
    },
    {
        "location": "Bromley",
        "postcode": "BR1 3NN",
        "price": "82210",
        "year": "96"
    }
]

第二个对象也需要添加数据。

下面是compiledData函数中提到的

const compiledData = [
    {
        "location": "City of London",
        "postcode": "EC1A 7BE",
        "price": "91449",
        "year": {
            "10": "",
            "11": "",
            "12": "",
            "13": "",
            "14": "",
            "15": "",
            "16": "",
            "17": "",
            "18": "",
            "19": "",
            "95": "",
            "96": "",
            "97": "",
            "98": "",
            "99": "",
            "00": "",
            "01": "",
            "02": "",
            "03": "",
            "04": "",
            "05": "",
            "06": "",
            "07": "",
            "08": "",
            "09": ""
        }
    },
    {
        "location": "Barking & Dagenham",
        "postcode": "RM9 4TP",
        "price": "50460",
        "year": {
            "10": "",
            "11": "",
            "12": "",
            "13": "",
            "14": "",
            "15": "",
            "16": "",
            "17": "",
            "18": "",
            "19": "",
            "95": "",
            "96": "",
            "97": "",
            "98": "",
            "99": "",
            "00": "",
            "01": "",
            "02": "",
            "03": "",
            "04": "",
            "05": "",
            "06": "",
            "07": "",
            "08": "",
            "09": ""
        }
    }
]

还有更多数据,但为简洁起见,这是一个简洁的版本。

标签: javascriptarraysobjectecmascript-6

解决方案


请参考以下代码片段。它可能会帮助你。我将原始数组视为复杂(变量名)并遍历它并将值存储在称为过滤的新字段中。

const complex = [{"location": "City of London", "postcode": "EC1A 7BE", "price": "91449", "year": "95"}, {"location": "Barking & Dagenham", "postcode": "RM9 4TP", "price": "50460", "year": "95"}, {"location": "Barnet", "postcode": "EN5 5RP", "price": "93285", "year": "95"}, {"location": "Bexley", "postcode": "DA16 3DN", "price": "64958", "year": "95"}, {"location": "Brent", "postcode": "HA9 9AA", "price": "71307", "year": "95"}, {"location": "Bromley", "postcode": "BR1 3NN", "price": "81671", "year": "95"}, {"location": "Camden", "postcode": "NW1 8QL", "price": "120933", "year": "95"}, {"location": "Croydon", "postcode": "CR2 7PA", "price": "69158", "year": "95"}, {"location": "Ealing", "postcode": "W13 0JX", "price": "79886", "year": "95"}, {"location": "Enfield", "postcode": "EN2 7AA", "price": "72515", "year": "95"}, {"location": "Greenwich", "postcode": "SE9 1DU", "price": "62300", "year": "95"}, {"location": "Hackney", "postcode": "E8 1FD", "price": "61297", "year": "95"}, {"location": "Hammersmith & Fulham", "postcode": "W6 8AB", "price": "124903", "year": "95"}, {"location": "Haringey", "postcode": "N8 9DJ", "price": "76288", "year": "95"}, {"location": "Harrow", "postcode": "HA3 5AG", "price": "84770", "year": "95"}, {"location": "Havering", "postcode": "RM12 6PL", "price": "68000", "year": "95"}, {"location": "Hillingdon", "postcode": "UB8 1JZ", "price": "73835", "year": "95"}, {"location": "Hounslow", "postcode": "TW3 2DL", "price": "72232", "year": "95"}, {"location": "Islington", "postcode": "N5 1RA", "price": "92516", "year": "95"}, {"location": "Kensington & Chelsea", "postcode": "W8 5SA", "price": "182695", "year": "95"}, {"location": "Kingston upon Thames", "postcode": "KT5 9NX", "price": "80876", "year": "95"}, {"location": "Lambeth", "postcode": "SW9 8HE", "price": "67771", "year": "95"}, {"location": "Lewisham", "postcode": "SE6 4RE", "price": "60491", "year": "95"}, {"location": "Merton", "postcode": "SW20 9JT", "price": "82071", "year": "95"}, {"location": "Newham", "postcode": "E13 9AP", "price": "53539", "year": "95"}, {"location": "Redbridge", "postcode": "IG6 1NA", "price": "72190", "year": "95"}, {"location": "Richmond upon Thames", "postcode": "TW9 1EZ", "price": "109326", "year": "95"}, {"location": "Southwark", "postcode": "SE1 5AA", "price": "67885", "year": "95"}, {"location": "Sutton", "postcode": "SM1 1JA", "price": "71537", "year": "95"}, {"location": "Tower Hamlets", "postcode": "E14 7JE", "price": "59865", "year": "95"}, {"location": "Waltham Forest", "postcode": "E17 7LP", "price": "61319", "year": "95"}, {"location": "Wandsworth", "postcode": "SW12 8PB", "price": "88559", "year": "95"}, {"location": "Westminster", "postcode": "SW1H 0BB", "price": "133025", "year": "95"}, {"location": "City of London", "postcode": "EC1A 7BE", "price": "108999", "year": "96"}, {"location": "Barking & Dagenham", "postcode": "RM9 4TP", "price": "50828", "year": "96"}, {"location": "Barnet", "postcode": "EN5 5RP", "price": "91111", "year": "96"}, {"location": "Bexley", "postcode": "DA16 3DN", "price": "63996", "year": "96"}, {"location": "Brent", "postcode": "HA9 9AA", "price": "72806", "year": "96"}, {"location": "Bromley", "postcode": "BR1 3NN", "price": "82210", "year": "96"}];

const filtered = [];    

complex.forEach( com => {
    if(filtered.length > 0 && filtered.filter( t => com.location === t.location).length > 0) {
        const index = filtered.findIndex( tt => tt.location === com.location);
        filtered[index]['year'][`${com['year']}`] = com['price']
    } else {
        let obj = {...com}
        obj['year'] = {[`${com['year']}`]: com['price']}
        filtered.push(obj);
    }
})
        
console.log(filtered);

回复请参考以下内容(Json 形式)

{
    "location": "City of London",
    "postcode": "EC1A 7BE",
    "price": "91449",
    "year": {
        "95": "91449",
        "96": "108999"
    }
}

推荐阅读