gpt4 book ai didi

javascript - 当两个不同对象的两个值匹配时,向对象添加新的键/对

转载 作者:行者123 更新时间:2023-12-02 22:56:02 25 4
gpt4 key购买 nike

我已经映射并过滤了两个对象。我的想法碰壁了。当值匹配时,我看不到如何将一个对象的键/值添加到另一个对象。

这种类型对我来说相当新鲜,并且耗尽了我的逻辑思维,因此任何帮助将不胜感激。

我想将“lat”和“lon”值以及“todaysDataPostcode”对象中的键添加到“data”对象中。

这应该在邮政编码匹配时完成。

我正在使用以前做过的方法,但最后阶段是不同的,而且事实证明很难弄清楚

代码如下:

addLatLon = (location, latLon) => {
return location.map(compiledDataObj => {
const objectsWithSameLocation = latLon.filter(dataObj => {
return dataObj.postcode === compiledDataObj.postcode;
});

objectsWithSameLocation.map(obj => {
//here we want to match the postcode of the object we're running through
// so its like
compiledDataObj.postcode[obj.postcode]

// return console.log('location.postcode', location.map(i => i.postcode), 'obj.postcode', obj.postcode)
});
});
};

这是 2 个数据集。第一个只是部分版本,因为这足以实现逻辑。

下面是:“数据”


[
{
"location": "City of London",
"postcode": "EC1A 7BE",
"year": {
"10": "464436",
"11": "442413",
"12": "525723",
"13": "465451",
"14": "625001",
"15": "783667",
"16": "736788",
"17": "820305",
"18": "802129",
"19": "864034",
"95": "91449",
"96": "108999",
"97": "116343",
"98": "124382",
"99": "149143",
"00": "173738",
"01": "284262",
"02": "344239",
"03": "261645",
"04": "326913",
"05": "330363",
"06": "316121",
"07": "360923",
"08": "471861",
"09": "400317"
}
},
{
"location": "Barking & Dagenham",
"postcode": "RM9 4TP",
"year": {
"10": "162905",
"11": "163821",
"12": "163899",
"13": "167919",
"14": "184884",
"15": "220070",
"16": "258758",
"17": "282441",
"18": "291548",
"19": "298333",
"95": "50460",
"96": "50828",
"97": "54459",
"98": "57559",
"99": "64532",
"00": "71079",
"01": "82343",
"02": "98713",
"03": "134750",
"04": "150115",
"05": "164484",
"06": "162340",
"07": "176577",
"08": "194235",
"09": "166798"
}
}]

这是第二个数据集:“todaysDataPostcode”

 [{
"postcode": "EC1A 7BE",
"longitude": -0.100404,
"latitude": 51.51775
},
{
"postcode": "RM9 4TP",
"longitude": 0.127884,
"latitude": 51.539774
},
{
"postcode": "EN5 5RP",
"longitude": -0.194074,
"latitude": 51.65044
},
{
"postcode": "DA16 3DN",
"longitude": 0.102535,
"latitude": 51.464737
},
{
"postcode": "HA9 9AA",
"longitude": -0.279243,
"latitude": 51.563322
},
{
"postcode": "BR1 3NN",
"longitude": 0.017131,
"latitude": 51.40827
},
{
"postcode": "NW1 8QL",
"longitude": -0.142696,
"latitude": 51.53929
},
{
"postcode": "CR2 7PA",
"longitude": -0.094105,
"latitude": 51.36292
}]

我想添加

如上所述,这是预期的结果,我无法达到结果,我现在迷失了。

“我想将“lat”和“lon”值以及“todaysDataPostcode”对象中的键添加到“data”对象中。

这应该在邮政编码匹配时完成。”

最佳答案

这就是你一直在追求的吗?

const arr1 = [{"location":"City of London","postcode":"EC1A 7BE","year":{"10":"464436","11":"442413","12":"525723","13":"465451","14":"625001","15":"783667","16":"736788","17":"820305","18":"802129","19":"864034","95":"91449","96":"108999","97":"116343","98":"124382","99":"149143","00":"173738","01":"284262","02":"344239","03":"261645","04":"326913","05":"330363","06":"316121","07":"360923","08":"471861","09":"400317"}},{"location":"Barking & Dagenham","postcode":"RM9 4TP","year":{"10":"162905","11":"163821","12":"163899","13":"167919","14":"184884","15":"220070","16":"258758","17":"282441","18":"291548","19":"298333","95":"50460","96":"50828","97":"54459","98":"57559","99":"64532","00":"71079","01":"82343","02":"98713","03":"134750","04":"150115","05":"164484","06":"162340","07":"176577","08":"194235","09":"166798"}}];

const arr2 = [{"postcode":"EC1A 7BE","longitude":-0.100404,"latitude":51.51775},{"postcode":"RM9 4TP","longitude":0.127884,"latitude":51.539774},{"postcode":"EN5 5RP","longitude":-0.194074,"latitude":51.65044},{"postcode":"DA16 3DN","longitude":0.102535,"latitude":51.464737},{"postcode":"HA9 9AA","longitude":-0.279243,"latitude":51.563322},{"postcode":"BR1 3NN","longitude":0.017131,"latitude":51.40827},{"postcode":"NW1 8QL","longitude":-0.142696,"latitude":51.53929},{"postcode":"CR2 7PA","longitude":-0.094105,"latitude":51.36292}];

const merged = arr1.map(e => (coords = arr2.find(ee => ee.postcode == e.postcode), Object.assign(e,{longitude:coords['longitude'],latitude:coords['latitude']})));

console.log(merged);
.as-console-wrapper{ min-height: 100% }

关于javascript - 当两个不同对象的两个值匹配时,向对象添加新的键/对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57975391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com