-3
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", …}
]

There are two like this, and I want to merge them based on the 'market'. Is there any way to merge?? I'm not sure because I'm a beginner in React.

  • Those are arrays of objects. React is a frontend framework and irrelevant here. –  May 06 '21 at 15:10
  • you can merge two objects using `{...obj1, obj2}`. Not quite sure what you mean `based on the 'market'.` – Shivam Jha May 06 '21 at 15:11
  • @ChrisG Oh array, my bad. – ABC May 06 '21 at 15:11
  • @ShivamJha Again, no. –  May 06 '21 at 15:11
  • use `a.concat(b)` then. **Update** can also use `const n = [...a, ...b]` – Shivam Jha May 06 '21 at 15:12
  • @ShivamJha Nope, still no. –  May 06 '21 at 15:14
  • 1
    `let combined = { ...a, ...b } console.log(a, b)`. It's an array, just output without array's contents. – ABC May 06 '21 at 15:14
  • @ABC No. Still badly wrong. –  May 06 '21 at 15:14
  • Solution: `const merged = a.map(curr => ({ ...curr, ...b.find(c => c.market === curr.market)}));` –  May 06 '21 at 15:15
  • what do you mean by no? See [merging two JS array](https://stackoverflow.com/a/66495523/11667949) – Shivam Jha May 06 '21 at 15:15
  • @ShivamJha This is not really about merging two arrays, it's about merging the objects inside them. See my solution. (It's actually easier than that, *if* the indices are always the same for both arrays) –  May 06 '21 at 15:16
  • @ChrisG You are some expert huh. **His code is not even properly formatted**. – ABC May 06 '21 at 15:18
  • @ABC It's not my fault if people keep getting it wrong. You're still posting objects after realizing the result has to be an array...? Somebody even upvoted your comment. What else am I supposed to do other than pointing out it's wrong? –  May 06 '21 at 15:20
  • Since when is `[ 0: {'value'}]` a proper `array/object` format. – ABC May 06 '21 at 15:23
  • @ABC OP probably copy pasted logged API responses from their console. Who cares? It's obvious what the input data is. But let's pretend they're objects: [still wrong](https://jsfiddle.net/Lu3nw49o/) –  May 06 '21 at 15:27
  • Then format the code properly. – ABC May 06 '21 at 15:35

2 Answers2

0

You can try this. There might be a better way to do this. Thank you.

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)
Showrin Barua
  • 1,737
  • 1
  • 11
  • 22
  • That's basically a copy of the accepted answer (and my comment), without devrnd's error checking. –  May 06 '21 at 16:42
-1

something like that:

const c = a.map(_a => {
    const foundB = b.find(_b => _a.market===_b.market)
    return foundB ? {..._a,...foundB} : _a
})
devrnd
  • 167
  • 5