3

I have object like this:

data = [{"id":"3d59db2c-b561-4978-89bf-9aea17f14e2d","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":10,"tncDescription":"Free 10x transfer to any bank","points":7000,"validity":"2022-01-08","status":true},
{"id":"57db1973-7e42-4992-a4f5-ce578fb78b8a","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":3,"tncDescription":"Free 3x transfer to any bank voucher","points":1500,"validity":"2021-10-08","status":true},
{"id":"eacb400d-827d-4ab4-b515-423dbb90d927","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":5,"tncDescription":"Free 5x transfer to any bank voucher","points":2000,"validity":"2021-10-08","status":true},
{"id":"7b4cde23-ffb1-4f94-8d74-d1be4b379342","category":"JALIN_OFFER","usage":"TOP_UP","quota":10,"tncDescription":"Free 10x top up e-wallet (OVO, GOPAY, Shopee Pay)","points":750,"validity":"2021-10-08","status":true},{"id":"92753ea2-0dc4-4605-a130-0c43f8f3b00f","category":"JALIN_OFFER","usage":"PAYMENT_BILL_PLN","quota":3,"tncDescription":"Free 3x PLN bill payment fee","points":1000,"validity":"2022-01-08","status":true},
{"id":"9dc7de52-1174-43a9-abc1-c9889e2982ba","category":"VOUCHER","usage":"PAYMENT_BILL_PDAM","quota":3,"tncDescription":"Free 3x PDAM bill payment fee","points":1000,"validity":"2022-01-08","status":true},
{"id":"dbe15d75-5550-49cb-8b5f-42aa85c2246f","category":"VOUCHER","usage":"PAYMENT_BILL_INTERNET","quota":3,"tncDescription":"Free 3x Indihome or FirstMedia or Biznet bill payment fee","points":1000,"validity":"2022-01-08","status":true}]

I have category Jalin Offer and Voucher

I want to split the object into 2 object with 2 category. 1 object with JALIN_OFFER category and 1 object with VOUCHER category

Things I have tried :

let voucher, offer = []    
data.forEach(element => {
            (element.category === "VOUCHER") ? 
            voucher.push(element)  : offer.push(element)
        });

This code make error : TypeError: Cannot read property 'push' of undefined

let voucher, offer = []
data.map((data,index)=> {
                (data.category === "VOUCHER") ? 
                voucher.push({
                    number:index+1,
                    ...data,
                }) : offer.push({
                    number:index+1,
                    ...data,
                })
            })

This code make error : TypeError: Cannot read property 'push' of undefined too

Any solution? Thank you before

1
  • 2
    you did not assign a value to voucher for starters. try let voucher = [], offer = [] Commented Jul 16, 2021 at 6:23

5 Answers 5

4

You can achieve this by destructuring:

data = [{"id":"3d59db2c-b561-4978-89bf-9aea17f14e2d","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":10,"tncDescription":"Free 10x transfer to any bank","points":7000,"validity":"2022-01-08","status":true},
{"id":"57db1973-7e42-4992-a4f5-ce578fb78b8a","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":3,"tncDescription":"Free 3x transfer to any bank voucher","points":1500,"validity":"2021-10-08","status":true},
{"id":"eacb400d-827d-4ab4-b515-423dbb90d927","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":5,"tncDescription":"Free 5x transfer to any bank voucher","points":2000,"validity":"2021-10-08","status":true},
{"id":"7b4cde23-ffb1-4f94-8d74-d1be4b379342","category":"JALIN_OFFER","usage":"TOP_UP","quota":10,"tncDescription":"Free 10x top up e-wallet (OVO, GOPAY, Shopee Pay)","points":750,"validity":"2021-10-08","status":true},{"id":"92753ea2-0dc4-4605-a130-0c43f8f3b00f","category":"JALIN_OFFER","usage":"PAYMENT_BILL_PLN","quota":3,"tncDescription":"Free 3x PLN bill payment fee","points":1000,"validity":"2022-01-08","status":true},
{"id":"9dc7de52-1174-43a9-abc1-c9889e2982ba","category":"VOUCHER","usage":"PAYMENT_BILL_PDAM","quota":3,"tncDescription":"Free 3x PDAM bill payment fee","points":1000,"validity":"2022-01-08","status":true},
{"id":"dbe15d75-5550-49cb-8b5f-42aa85c2246f","category":"VOUCHER","usage":"PAYMENT_BILL_INTERNET","quota":3,"tncDescription":"Free 3x Indihome or FirstMedia or Biznet bill payment fee","points":1000,"validity":"2022-01-08","status":true}]

const [offer,voucher] = [data.filter(x => x.category === "JALIN_OFFER"),data.filter(x => x.category === "VOUCHER")]

console.log(voucher,'----',offer)

Sign up to request clarification or add additional context in comments.

Comments

3

Variable voucher is not initialized properly. Try initializing it separately and try again.

let voucher = [];
let offer = [];
...

Comments

2

assign [] to voucher

let voucher = [], offer = [];  

data.forEach(element => {
            (element.category === "VOUCHER") ? 
            voucher.push(element)  : offer.push(element)
        });

Comments

2

With let voucher, offer = [] offer is empty array [] and voucher is undefined

data = [{"id":"3d59db2c-b561-4978-89bf-9aea17f14e2d","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":10,"tncDescription":"Free 10x transfer to any bank","points":7000,"validity":"2022-01-08","status":true},
{"id":"57db1973-7e42-4992-a4f5-ce578fb78b8a","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":3,"tncDescription":"Free 3x transfer to any bank voucher","points":1500,"validity":"2021-10-08","status":true},
{"id":"eacb400d-827d-4ab4-b515-423dbb90d927","category":"JALIN_OFFER","usage":"TRANSFER_DOMESTIC","quota":5,"tncDescription":"Free 5x transfer to any bank voucher","points":2000,"validity":"2021-10-08","status":true},
{"id":"7b4cde23-ffb1-4f94-8d74-d1be4b379342","category":"JALIN_OFFER","usage":"TOP_UP","quota":10,"tncDescription":"Free 10x top up e-wallet (OVO, GOPAY, Shopee Pay)","points":750,"validity":"2021-10-08","status":true},{"id":"92753ea2-0dc4-4605-a130-0c43f8f3b00f","category":"JALIN_OFFER","usage":"PAYMENT_BILL_PLN","quota":3,"tncDescription":"Free 3x PLN bill payment fee","points":1000,"validity":"2022-01-08","status":true},
{"id":"9dc7de52-1174-43a9-abc1-c9889e2982ba","category":"VOUCHER","usage":"PAYMENT_BILL_PDAM","quota":3,"tncDescription":"Free 3x PDAM bill payment fee","points":1000,"validity":"2022-01-08","status":true},
{"id":"dbe15d75-5550-49cb-8b5f-42aa85c2246f","category":"VOUCHER","usage":"PAYMENT_BILL_INTERNET","quota":3,"tncDescription":"Free 3x Indihome or FirstMedia or Biznet bill payment fee","points":1000,"validity":"2022-01-08","status":true}]

let voucher = []
let offer = []    
data.forEach(element => {
            return (element.category === "VOUCHER") ? 
            voucher.push(element) : offer.push(element)
 });
 console.log(voucher )
 console.log(offer)

Comments

1

Change let voucher, offer = [] to let voucher = []; let offer = [];

data.forEach(element =>  (element.category === "VOUCHER") ?   voucher.push(element)  : offer.push(element));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.