@Impuls3

Как разбить объект на несколько частей?

Есть объект:
let items = {
    '630868787': {
        price: 697.63,
        id: '630868787',
        name: 'M4A1-S | Hyper Beast (Well-Worn)',
        min_price: null
    },
    '630868790': {
        price: 439.59,
        id: '630868790',
        name: 'StatTrak™ Five-SeveN | Monkey Business (Field-Tested)',
        min_price: null
    },
    '630868805': {
        price: 792,
        id: '630868805',
        name: 'M4A4 | Desolate Space (Battle-Scarred)',
        min_price: null
    },
    '630868817': {
        price: 254.43,
        id: '630868817',
        name: 'StatTrak™ AK-47 | Uncharted (Field-Tested)',
        min_price: null
    },
    '630868829': {
        price: 280.71,
        id: '630868829',
        name: 'Glock-18 | Brass (Field-Tested)',
        min_price: null
    },
    '630868859': {
        price: 268.77,
        id: '630868859',
        name: 'MP7 | Bloodsport (Battle-Scarred)',
        min_price: null
    },
    '630868907': {
        price: 310.57,
        id: '630868907',
        name: 'CZ75-Auto | Xiangliu (Battle-Scarred)',
        min_price: null
    },
    '630868919': {
        price: 315.36,
        id: '630868919',
        name: 'AK-47 | Predator (Minimal Wear)',
        min_price: null
    },
    '630868931': {
        price: 358.36,
        id: '630868931',
        name: 'M4A4 | Griffin (Battle-Scarred)',
        min_price: null
    },
    '630868958': {
        price: 299.84,
        id: '630868958',
        name: 'StatTrak™ USP-S | Guardian (Field-Tested)',
        min_price: null
    },
    '630868967': {
        price: 323.71,
        id: '630868967',
        name: 'M4A1-S | Leaded Glass (Minimal Wear)',
        min_price: null
    },
    '630868979': {
        price: 554.28,
        id: '630868979',
        name: 'USP-S | Caiman (Minimal Wear)',
        min_price: null
    },
    '630868988': {
        price: 502.91,
        id: '630868988',
        name: 'P250 | See Ya Later (Well-Worn)',
        min_price: null
    },
    '630869003': {
        price: 549.49,
        id: '630869003',
        name: 'StatTrak™ M4A1-S | Leaded Glass (Battle-Scarred)',
        min_price: null
    },
    '630869021': {
        price: 254.43,
        id: '630869021',
        name: 'StatTrak™ AK-47 | Uncharted (Field-Tested)',
        min_price: null
    },
    '630869030': {
        price: 268.77,
        id: '630869030',
        name: 'MP7 | Bloodsport (Battle-Scarred)',
        min_price: null
    },
    '630869048': {
        price: 268.77,
        id: '630869048',
        name: 'MP7 | Bloodsport (Battle-Scarred)',
        min_price: null
    },
    '630869057': {
        price: 310.57,
        id: '630869057',
        name: 'CZ75-Auto | Xiangliu (Battle-Scarred)',
        min_price: null
    },
    '630869063': {
        price: 310.57,
        id: '630869063',
        name: 'CZ75-Auto | Xiangliu (Battle-Scarred)',
        min_price: null
    },
    '630869078': {
        price: 358.36,
        id: '630869078',
        name: 'M4A4 | Griffin (Battle-Scarred)',
        min_price: null
    },
    '630869081': {
        price: 358.36,
        id: '630869081',
        name: 'M4A4 | Griffin (Battle-Scarred)',
        min_price: null
    },
    '630869099': {
        price: 353.59,
        id: '630869099',
        name: 'Desert Eagle | Mecha Industries (Battle-Scarred)',
        min_price: null
    },
    '630869111': {
        price: 310.57,
        id: '630869111',
        name: 'AWP | Elite Build (Field-Tested)',
        min_price: null
    },
    '630869123': {
        price: 329.7,
        id: '630869123',
        name: 'Glock-18 | Water Elemental (Battle-Scarred)',
        min_price: null
    },
    '630869144': {
        price: 341.64,
        id: '630869144',
        name: 'FAMAS | Roll Cage (Well-Worn)',
        min_price: null
    },
    '630869147': {
        price: 341.64,
        id: '630869147',
        name: 'FAMAS | Roll Cage (Well-Worn)',
        min_price: null
    },
    '630869162': {
        price: 344.03,
        id: '630869162',
        name: 'Desert Eagle | Midnight Storm (Minimal Wear)',
        min_price: null
    },
    '630869168': {
        price: 344.03,
        id: '630869168',
        name: 'Desert Eagle | Midnight Storm (Minimal Wear)',
        min_price: null
    },
    '630869171': {
        price: 384.64,
        id: '630869171',
        name: 'USP-S | Cortex (Well-Worn)',
        min_price: null
    },
    '630869174': {
        price: 384.64,
        id: '630869174',
        name: 'USP-S | Cortex (Well-Worn)',
        min_price: null
    },
    '630869183': {
        price: 409.73,
        id: '630869183',
        name: 'M4A4 | Griffin (Minimal Wear)',
        min_price: null
    },
    '630869195': {
        price: 439.59,
        id: '630869195',
        name: 'StatTrak™ Five-SeveN | Monkey Business (Field-Tested)',
        min_price: null
    },
    '630869228': {
        price: 527.99,
        id: '630869228',
        name: 'MAC-10 | Neon Rider (Field-Tested)',
        min_price: null
    },
    '630869246': {
        price: 697.63,
        id: '630869246',
        name: 'M4A1-S | Hyper Beast (Well-Worn)',
        min_price: null
    },
    '630869282': {
        price: 1032.1,
        id: '630869282',
        name: 'M4A4 | Hellfire (Field-Tested)',
        min_price: null
    },
    '630869291': {
        price: 1032.1,
        id: '630869291',
        name: 'M4A4 | Hellfire (Field-Tested)',
        min_price: null
    },
    '630869324': {
        price: 1177.84,
        id: '630869324',
        name: 'StatTrak™ P90 | Asiimov (Battle-Scarred)',
        min_price: null
    },
    '630869339': {
        price: 266.38,
        id: '630869339',
        name: 'StatTrak™ Desert Eagle | Directive (Well-Worn)',
        min_price: null
    },
    '630869354': {
        price: 752.57,
        id: '630869354',
        name: 'Desert Eagle | Mecha Industries (Factory New)',
        min_price: null
    },
    '630869375': {
        price: 821.85,
        id: '630869375',
        name: 'StatTrak™ M4A1-S | Nightmare (Battle-Scarred)',
        min_price: null
    },
    '630869387': {
        price: 821.85,
        id: '630869387',
        name: 'StatTrak™ M4A1-S | Nightmare (Battle-Scarred)',
        min_price: null
    },
    '630869399': {
        price: 20.31,
        id: '630869399',
        name: 'Desert Eagle | Urban Rubble (Well-Worn)',
        min_price: null
    },
    '630869408': {
        price: 59.73,
        id: '630869408',
        name: 'StatTrak™ UMP-45 | Briefing (Minimal Wear)',
        min_price: null
    },
    '630869423': {
        price: 63.31,
        id: '630869423',
        name: 'PP-Bizon | Fuel Rod (Battle-Scarred)',
        min_price: null
    },
    '630869426': {
        price: 145.73,
        id: '630869426',
        name: 'USP-S | Flashback (Well-Worn)',
        min_price: null
    },
    '630869435': {
        price: 145.73,
        id: '630869435',
        name: 'USP-S | Flashback (Well-Worn)',
        min_price: null
    },
    '630869441': {
        price: 145.73,
        id: '630869441',
        name: 'USP-S | Flashback (Well-Worn)',
        min_price: null
    },
    '630869453': {
        price: 182.77,
        id: '630869453',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869462': {
        price: 182.77,
        id: '630869462',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869474': {
        price: 182.77,
        id: '630869474',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869498': {
        price: 182.77,
        id: '630869498',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869516': {
        price: 182.77,
        id: '630869516',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869522': {
        price: 182.77,
        id: '630869522',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869528': {
        price: 182.77,
        id: '630869528',
        name: 'StatTrak™ M4A4 | Magnesium (Well-Worn)',
        min_price: null
    },
    '630869537': {
        price: 90.78,
        id: '630869537',
        name: 'StatTrak™ Five-SeveN | Violent Daimyo (Minimal Wear)',
        min_price: null
    },
    '630869561': {
        price: 64.5,
        id: '630869561',
        name: 'MP9 | Setting Sun (Well-Worn)',
        min_price: null
    }
}

В этом объекте 56 объектов.
Надо сделать из них массив, так, чтобы в каждый элемент массива содержал не более 10 объектов:

let newArr = [
    {
        '1241251251': {...},
        '1241251252': {...},
        '1241251253': {...}, 
        //more 7...      
    },
    {
        '1241251254': {...},
        '1241251255': {...},
        '1241251256': {...}, 
        //more 7...   
    }
]
  • Вопрос задан
  • 332 просмотра
Решения вопроса 1
0xD34F
@0xD34F Куратор тега JavaScript
const chunked = (arr, chunkSize) =>
  Array.from(
    { length: Math.ceil(arr.length / chunkSize) },
    (n, i) => arr.slice(i * chunkSize, (i + 1) * chunkSize)
  );


const arr = chunked(Object.entries(obj), 10).map(Object.fromEntries);
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Seasle
@Seasle Куратор тега JavaScript
И в чём проблема? Object.entries (Ну или for ... in / for ... of Object.keys(...)). Ну и всё. Только считайте количество и кладите в новые массивы.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
26 нояб. 2024, в 21:30
30000 руб./за проект
26 нояб. 2024, в 20:38
120000 руб./за проект
26 нояб. 2024, в 20:08
1000 руб./за проект