S.M_Emamian
S.M_Emamian

Reputation: 17383

How to find out similar objects in array - react js

I have seen similar questions about duplicate objects.

my json output:

{    "coupons": [
        {
            "id": "376363",
            "price": "14400",
            "date": "2018-04-08 10:40:17",
            "user_id": "16433",
            "ip": "46.225.123.235",
            "code": "5ac9b249a0cc5",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "20821",
            "coupon_parent": "20821",
            "coupon_code": "195_2484C1_9873(7531)",
            "coupon_code_user": "7531",
            "coupon_code_partner": "195_2484C1_9873",
            "shop_id": "2484",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2018-03-24 05:05:05",
            "cradit_end_date": "2018-04-22 05:05:05",
            "expired": "0",
            "pay_data": null,
            "seri": "C",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "5ac9b249970c2",
            "coupon_property_id": "0",
            "title": "title1",
            "coupon_property_title": "",
            "parent_title": ""
        },
        {
            "id": "376362",
            "price": "14400",
            "date": "2018-04-08 10:40:17",
            "user_id": "16433",
            "ip": "46.225.123.235",
            "code": "5ac9b24997103",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "20821",
            "coupon_parent": "20821",
            "coupon_code": "194_2484C1_9779(4478)",
            "coupon_code_user": "4478",
            "coupon_code_partner": "194_2484C1_9779",
            "shop_id": "2484",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2018-03-24 05:05:05",
            "cradit_end_date": "2018-04-22 05:05:05",
            "expired": "0",
            "pay_data": null,
            "seri": "C",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "5ac9b249970c2",
            "coupon_property_id": "0",
            "title": "title2",
            "coupon_property_title": "",
            "parent_title": ""
        },
        {
            "id": "341459",
            "price": "27000",
            "date": "2017-03-07 10:42:47",
            "user_id": "16433",
            "ip": "46.225.76.21",
            "code": "58be5d6fd7214",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "19457",
            "coupon_parent": "19457",
            "coupon_code": "7_1310B1_2389(3386)",
            "coupon_code_user": "3386",
            "coupon_code_partner": "7_1310B1_2389",
            "shop_id": "1310",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2017-01-16 05:05:05",
            "cradit_end_date": "2017-03-19 05:05:05",
            "expired": "11",
            "pay_data": null,
            "seri": "B",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "58be5d6fd71c6",
            "coupon_property_id": "0",
            "title": "title3",
            "coupon_property_title": "",
            "parent_title": ""
        },
        {
            "id": "341456",
            "price": "11250",
            "date": "2017-03-07 10:34:54",
            "user_id": "16433",
            "ip": "46.225.76.21",
            "code": "58be5b964bf1d",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "19724",
            "coupon_parent": "19724",
            "coupon_code": "16_2129A1_2178(4663)",
            "coupon_code_user": "4663",
            "coupon_code_partner": "16_2129A1_2178",
            "shop_id": "2129",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2017-03-05 05:05:05",
            "cradit_end_date": "2017-05-05 05:05:05",
            "expired": "11",
            "pay_data": null,
            "seri": "A",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "58be5b964b1a1",
            "coupon_property_id": "0",
            "title": "title4",
            "coupon_property_title": "",
            "parent_title": ""
        }
]
}

each object has a buy_id field. buy_id shows that the objects are the same and I should print them inside one row of html table.

for example the first object and the second object are the same depending on buy_id I print them inside row. output:

---------
title1 - title2 //because the first object and second object have same `buy_id`
---------
title3
---------
title4

should I use two maps?

I should use this scenario inside return of render function.

Upvotes: 0

Views: 101

Answers (2)

benjamin Rampon
benjamin Rampon

Reputation: 1416

You have to use reduce :

data.coupons.reduce((acc,value)=>{
  return acc[value.buy_id] === undefined
    ? {...acc,[value.buy_id]:value.title}
    : {...acc,[value.buy_id]:`${acc[value.buy_id]} - ${value.title}`}
},{})

The result is :

{
    "5ac9b249970c2": "title1 - title2",
    "58be5d6fd71c6": "title3",
    "58be5b964b1a1": "title4"
}

Upvotes: 0

ben
ben

Reputation: 3568

I put a bet on reduce for that.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

the following snippets will group your objects by buy_id, then you can render them easily

const data = {    "coupons": [
        {
            "id": "376363",
            "price": "14400",
            "date": "2018-04-08 10:40:17",
            "user_id": "16433",
            "ip": "46.225.123.235",
            "code": "5ac9b249a0cc5",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "20821",
            "coupon_parent": "20821",
            "coupon_code": "195_2484C1_9873(7531)",
            "coupon_code_user": "7531",
            "coupon_code_partner": "195_2484C1_9873",
            "shop_id": "2484",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2018-03-24 05:05:05",
            "cradit_end_date": "2018-04-22 05:05:05",
            "expired": "0",
            "pay_data": null,
            "seri": "C",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "5ac9b249970c2",
            "coupon_property_id": "0",
            "title": "title1",
            "coupon_property_title": "",
            "parent_title": ""
        },
        {
            "id": "376362",
            "price": "14400",
            "date": "2018-04-08 10:40:17",
            "user_id": "16433",
            "ip": "46.225.123.235",
            "code": "5ac9b24997103",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "20821",
            "coupon_parent": "20821",
            "coupon_code": "194_2484C1_9779(4478)",
            "coupon_code_user": "4478",
            "coupon_code_partner": "194_2484C1_9779",
            "shop_id": "2484",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2018-03-24 05:05:05",
            "cradit_end_date": "2018-04-22 05:05:05",
            "expired": "0",
            "pay_data": null,
            "seri": "C",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "5ac9b249970c2",
            "coupon_property_id": "0",
            "title": "title2",
            "coupon_property_title": "",
            "parent_title": ""
        },
        {
            "id": "341459",
            "price": "27000",
            "date": "2017-03-07 10:42:47",
            "user_id": "16433",
            "ip": "46.225.76.21",
            "code": "58be5d6fd7214",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "19457",
            "coupon_parent": "19457",
            "coupon_code": "7_1310B1_2389(3386)",
            "coupon_code_user": "3386",
            "coupon_code_partner": "7_1310B1_2389",
            "shop_id": "1310",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2017-01-16 05:05:05",
            "cradit_end_date": "2017-03-19 05:05:05",
            "expired": "11",
            "pay_data": null,
            "seri": "B",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "58be5d6fd71c6",
            "coupon_property_id": "0",
            "title": "title3",
            "coupon_property_title": "",
            "parent_title": ""
        },
        {
            "id": "341456",
            "price": "11250",
            "date": "2017-03-07 10:34:54",
            "user_id": "16433",
            "ip": "46.225.76.21",
            "code": "58be5b964bf1d",
            "succ": "1",
            "admin_seen": "1",
            "user_seen": "0",
            "coupon_id": "19724",
            "coupon_parent": "19724",
            "coupon_code": "16_2129A1_2178(4663)",
            "coupon_code_user": "4663",
            "coupon_code_partner": "16_2129A1_2178",
            "shop_id": "2129",
            "payment_type": "7",
            "merchent_type": "3",
            "merchent_id": "0",
            "cradit_start_date": "2017-03-05 05:05:05",
            "cradit_end_date": "2017-05-05 05:05:05",
            "expired": "11",
            "pay_data": null,
            "seri": "A",
            "to_friend": "0",
            "finance_id": "0",
            "app": "web",
            "expire_date": "0000-00-00 00:00:00",
            "expire_app": "",
            "buy_id": "58be5b964b1a1",
            "coupon_property_id": "0",
            "title": "title4",
            "coupon_property_title": "",
            "parent_title": ""
        }
]
};


const reducedData = data.coupons.reduce((accumulator, value) => {
  accumulator[value.buy_id] = accumulator[value.buy_id] || [];
  accumulator[value.buy_id].push(value);
  return accumulator;
}, {});

console.log(reducedData);

Upvotes: 3

Related Questions