1

Есть большой объект (json с данными), подскажите, как из него получить значения только уникальных значений. Вот пример.

let result = {
  "items": [
    [
      [
        "2190805815332573217",
        "desc"
      ],
      {
        "id": "2190805815332573217",
        "scheduled": 1666539000,
        "type": "match",
        "virtual": false,
        "sport": {
          "id": "1",
          "name": "Football"
        },
        "category": {
          "id": "1669818934880243712",
          "name": "Germany",
          "sport_id": "1",
          "country_code": "DEU"
        },
        "tournament": {
          "id": "1669818934959935488",
          "name": "Bundesliga",
          "category_id": "1669818934880243712"
        },
        "competitors": [{
            "id": "2528",
            "name": "Hertha BSC",
            "sport_id": "1",
            "country_code": "DEU",
            "abbreviation": "BSC"
          },
          {
            "id": "2530",
            "name": "Schalke 04",
            "sport_id": "1",
            "country_code": "DEU",
            "abbreviation": "SCH"
          }
        ]
      }
    ],
    [
      [
        "2190805815332573217",
        "market",
        "1"
      ],
      {
        "": {
          "1": {
            "k": "1.75"
          },
          "2": {
            "k": "4.0"
          },
          "3": {
            "k": "4.6"
          }
        }
      }
    ],
    [
      [
        "2193681727040397350",
        "desc"
      ],
      {
        "id": "2193681727040397350",
        "scheduled": 1666290600,
        "type": "match",
        "virtual": false,
        "sport": {
          "id": "6",
          "name": "Handball"
        },
        "category": {
          "id": "1669818926353223680",
          "name": "Denmark",
          "sport_id": "6",
          "country_code": "DNK"
        },
        "tournament": {
          "id": "1669818926403555328",
          "name": "Handboldligaen",
          "category_id": "1669818926353223680"
        },
        "competitors": [{
            "id": "5644",
            "name": "Mors-Thy Haandbold",
            "sport_id": "6",
            "country_code": "DNK",
            "abbreviation": "MTH"
          },
          {
            "id": "4095",
            "name": "Fredericia HK 1990",
            "sport_id": "6",
            "country_code": "DNK",
            "abbreviation": "FRE"
          }
        ]
      }
    ],
    [
      [
        "2193681727040397350",
        "market",
        "1"
      ],
      {
        "": {
          "1": {
            "k": "2.49"
          },
          "2": {
            "k": "8.0"
          },
          "3": {
            "k": "1.81"
          }
        }
      }
    ],
    [
            [
                "2193461880452943890",
                "desc"
            ],
            {
                "id": "2193461880452943890",
                "scheduled": 1666454400,
                "type": "match",
                "virtual": false,
                "sport": {
                    "id": "16",
                    "name": "American Football"
                },
                "category": {
                    "id": "1669818823089459200",
                    "name": "USA",
                    "sport_id": "16",
                    "country_code": "USA"
                },
                "tournament": {
                    "id": "1685766595592462336",
                    "name": "NCAA, Regular Season",
                    "category_id": "1669818823089459200"
                },
                "competitors": [
                    {
                        "id": "4359",
                        "name": "Baylor Bears",
                        "sport_id": "16",
                        "country_code": "USA",
                        "abbreviation": "BAY"
                    },
                    {
                        "id": "4369",
                        "name": "Kansas Jayhawks",
                        "sport_id": "16",
                        "country_code": "USA",
                        "abbreviation": "KU"
                    }
                ]
            }
        ],
    [
      [
        "2193779596028157967",
        "market",
        "1"
      ],
      {
        "": {
          "1": {
            "k": "2.65"
          },
          "2": {
            "k": "3.0"
          },
          "3": {
            "k": "2.55"
          }
        }
      }
    ],
    [
      [
        "2193779596028157967",
        "market",
        "11"
      ],
      {
        "": {
          "4": {
            "k": "1.88"
          },
          "5": {
            "k": "1.83"
          }
        }
      }
    ],
    [
      [
        "2194882844722077708",
        "desc"
      ],
      {
        "id": "2194882844722077708",
        "scheduled": 1666283785,
        "type": "match",
        "virtual": true,
        "sport": {
          "id": "302",
          "name": "NBA 2K"
        },
        "category": {
          "id": "2014287224836853760",
          "name": "USA",
          "sport_id": "302",
          "country_code": "USA"
        },
        "tournament": {
          "id": "2014289567552774144",
          "name": "NBA Current",
          "category_id": "2014287224836853760"
        },
        "competitors": [{
            "id": "2014360357749002240",
            "name": "Philadelphia 76ers",
            "sport_id": "302"
          },
          {
            "id": "2014315775527100416",
            "name": "Golden State Warriors",
            "sport_id": "302"
          }
        ]
      }
    ],
    [
      [
        "2194882844722077708",
        "market",
        "1"
      ],
      {
        "": {
          "1": {
            "k": "4.9"
          },
          "2": {
            "k": "0.0",
            "b": 1
          },
          "3": {
            "k": "1.17"
          }
        }
      }
    ],
    [
      [
        "2191279964345737226",
        "desc"
      ],
      {
        "id": "2191279964345737226",
        "scheduled": 1666537200,
        "type": "match",
        "virtual": false,
        "sport": {
          "id": "1",
          "name": "Football"
        },
        "category": {
          "id": "1669818800066924544",
          "name": "Norway",
          "sport_id": "1",
          "country_code": "NOR"
        },
      }
    ]
  ]
}


function getAllSporsList(){
  let set = new Set(result)
   let arr = [];
   for (let unicItem of set) {
        arr.push = unicItem.sport.name; 
        }
  return arr
}

console.log(getAllSporsList());

https://jsfiddle.net/qhd79ocg/ Задача состоит в том, что бы получить в массив только уникальные значения sport.name (пример "Football", "American Football", "Handball" ... ) Поскольку объект с данными очень большой, пробую решить задачу через Set Но, что-то не получается. Подскажите, как это решить ?

1 ответ 1

2

Попробуйте сделать вот так, если я вас правильно понял. Пробежался по массиву result и добавлял sport.name в Set. В конце вернул массив

function getAllSporsList(result){
    let set = new Set()
    for (const item of result.items) {
        for(const subItem of item) {
            if (typeof subItem === 'object' && 'sport' in subItem) {
                set.add(subItem.sport.name)
            }
        }
    }
    return [...set]
}

console.log(getAllSporsList(result));
3
  • Спасибо. А можно ещё вопрос, а если необходимо сделать в конечном итоге объект из значений { 'sport.id1' : 'sport.name1', 'sport.id2' : 'sport.name2' ... } тоже уникальных значений, как необходимо видоизменить функцию ? 20 окт 2022 в 18:33
  • 1
    Можно в set добавить целый объект sport. Измените ту линию которое добавляет name в set вот так set.add(subItem.sport). И чтобы получился точно так как вы указали можно еще одну функцию сделать: https://jsfiddle.net/kqem23u4/ Про reduce можете почитать - методы для массива. И про {...obj} spread operator
    – Azamat
    21 окт 2022 в 4:34
  • Спасибо огромное 21 окт 2022 в 8:05

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.