如何组合两个反应对象?

Posted

技术标签:

【中文标题】如何组合两个反应对象?【英文标题】:How can I combine two react objects? 【发布时间】:2021-07-28 22:26:06 【问题描述】:
const a = [

0: market: "KRW-BTC", korean_name: "비트코인", english_name: "Bitcoin"
1: market: "KRW-ETH", korean_name: "이더리움", english_name: "Ethereum"
2: market: "KRW-NEO", korean_name: "네오", english_name: "NEO"
3: market: "KRW-MTL", korean_name: "메탈", english_name: "Metal"
4: market: "KRW-LTC", korean_name: "라이트코인", english_name: "Litecoin"
5: market: "KRW-XRP", korean_name: "리플", english_name: "Ripple"
6: market: "KRW-ETC", korean_name: "이더리움클래식", english_name: "Ethereum Classic"
7: market: "KRW-OMG", korean_name: "오미세고", english_name: "OmiseGo"
8: market: "KRW-SNT", korean_name: "스테이터스네트워크토큰", english_name: "Status Network Token"
9: market: "KRW-WAVES", korean_name: "웨이브", english_name: "Waves"
10: market: "KRW-XEM", korean_name: "넴", english_name: "NEM"
11: market: "KRW-QTUM", korean_name: "퀀텀", english_name: "Qtum"
12: market: "KRW-LSK", korean_name: "리스크", english_name: "Lisk"
]



const b = [

0: market: "KRW-BTC", trade_date: "20210506", trade_time: "144435", trade_date_kst: "20210506", trade_time_kst: "234435", …
1: market: "KRW-ETH", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
2: market: "KRW-NEO", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
3: market: "KRW-MTL", trade_date: "20210506", trade_time: "144432", trade_date_kst: "20210506", trade_time_kst: "234432", …
4: market: "KRW-LTC", trade_date: "20210506", trade_time: "144433", trade_date_kst: "20210506", trade_time_kst: "234433", …
5: market: "KRW-XRP", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
6: market: "KRW-ETC", trade_date: "20210506", trade_time: "144437", trade_date_kst: "20210506", trade_time_kst: "234437", …
7: market: "KRW-OMG", trade_date: "20210506", trade_time: "144437", trade_date_kst: "20210506", trade_time_kst: "234437", …
8: market: "KRW-SNT", trade_date: "20210506", trade_time: "144434", trade_date_kst: "20210506", trade_time_kst: "234434", …
9: market: "KRW-WAVES", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
10: market: "KRW-XEM", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
11: market: "KRW-QTUM", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
12: market: "KRW-LSK", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436", …
]

有两个这样的,我想根据“市场”合并它们。有什么办法合并吗??我不确定,因为我是 React 的初学者。

【问题讨论】:

这些是对象数组。 React 是一个前端框架,在这里无关紧要。 您可以使用...obj1, obj2 合并两个对象。不太清楚你的意思based on the 'market'. @ChrisG 哦,数组,我的错。 @ShivamJha 再次,不。 let combined = ...a, ...b console.log(a, b)。它是一个数组,只是输出没有数组的内容。 【参考方案1】:

类似的东西:

const c = a.map(_a => 
    const foundB = b.find(_b => _a.market===_b.market)
    return foundB ? ..._a,...foundB : _a
)

【讨论】:

非常感谢。谢谢。非常感谢您的帮助。【参考方案2】:

你可以试试这个。可能有更好的方法来做到这一点。谢谢。

const a = [
  market: "KRW-BTC", korean_name: "비트코인", english_name: "Bitcoin",
  market: "KRW-ETH", korean_name: "이더리움", english_name: "Ethereum",
  market: "KRW-NEO", korean_name: "네오", english_name: "NEO",
  market: "KRW-MTL", korean_name: "메탈", english_name: "Metal",
  market: "KRW-LTC", korean_name: "라이트코인", english_name: "Litecoin",
  market: "KRW-XRP", korean_name: "리플", english_name: "Ripple",
  market: "KRW-ETC", korean_name: "이더리움클래식", english_name: "Ethereum Classic",
  market: "KRW-OMG", korean_name: "오미세고", english_name: "OmiseGo",
  market: "KRW-SNT", korean_name: "스테이터스네트워크토큰", english_name: "Status Network Token",
  market: "KRW-WAVES", korean_name: "웨이브", english_name: "Waves",
  market: "KRW-XEM", korean_name: "넴", english_name: "NEM",
  market: "KRW-QTUM", korean_name: "퀀텀", english_name: "Qtum",
  market: "KRW-LSK", korean_name: "리스크", english_name: "Lisk",
]

const b = [
  market: "KRW-BTC", trade_date: "20210506", trade_time: "144435", trade_date_kst: "20210506", trade_time_kst: "234435",
  market: "KRW-ETH", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
  market: "KRW-NEO", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
  market: "KRW-MTL", trade_date: "20210506", trade_time: "144432", trade_date_kst: "20210506", trade_time_kst: "234432",
  market: "KRW-LTC", trade_date: "20210506", trade_time: "144433", trade_date_kst: "20210506", trade_time_kst: "234433",
  market: "KRW-XRP", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
  market: "KRW-ETC", trade_date: "20210506", trade_time: "144437", trade_date_kst: "20210506", trade_time_kst: "234437",
  market: "KRW-OMG", trade_date: "20210506", trade_time: "144437", trade_date_kst: "20210506", trade_time_kst: "234437",
  market: "KRW-SNT", trade_date: "20210506", trade_time: "144434", trade_date_kst: "20210506", trade_time_kst: "234434",
  market: "KRW-WAVES", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
  market: "KRW-XEM", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
  market: "KRW-QTUM", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
  market: "KRW-LSK", trade_date: "20210506", trade_time: "144436", trade_date_kst: "20210506", trade_time_kst: "234436",
]

const finalArray = a.map(item => (...item, ...b.find(itemOfB => itemOfB.market === item.market)))

console.log(finalArray)

【讨论】:

这基本上是接受的答案(和我的评论)的副本,没有 devrnd 的错误检查。

以上是关于如何组合两个反应对象?的主要内容,如果未能解决你的问题,请参考以下文章

如何正确组合两个实时数据对象

2020-12-30 两个数组合并并去重对象

检查两个数组是不是包含相同的对象 - 反应 componentDidUpdate [重复]

C ++如何将两个makefile对象目标规则(位于另一个文件夹中)组合到一个目标/规则中?

如何根据javascript中的键合并和替换两个数组中的对象?

反应js组合