Quick actions

cmd+k|ctrl+k

Navigation

Languages

spread obejct array

Snippet info

Language

JavaScript

Visibility

public

Author

asepkhabril20

Created

2024-11-06T03:53:13.566394Z

Updated

2024-11-06T03:53:13.566394Z

const listLaptop = [{
    Processor: "I7 13700",
    GPU: "RTX 4060",
    Merk: "Lenovo",
    RAM: "16",
    Internal:"512",
    Type: "Legion Slim 5i",
    Harga: 23_000_000
},{
    Processor: "Ryzen 7840HS",
    GPU: "RTX 4060",
    Merk: "Lenovo",
    RAM: "16",
    Internal:"512",
    Type: "Legion Slim 5",
    Harga: 22_000_000
}]

const laptopLenovo ={
    Processor: "i9 13900",
    GPU: "RTX 4080",
    Merk: "Lenovo",
    RAM: "32",
    Internal:"2000",
    Type: "Legion 5i Pro",
    Harga: 38_099_000
}
const laptopSpread = [laptopLenovo,...listLaptop,];

//  # akan tertimpa
// const laptopSpread2 = {
//     ...listLaptop[0], // Mengambil objek pertama di listLaptop
//     ...listLaptop[1], // Mengambil objek kedua di listLaptop
//     ...laptopLenovo   // Menggabungkan objek laptopLenovo
// };

// const laptopSpread2 = listLaptop.reduce((acc, obj) => ({ ...acc, ...obj }), { ...laptopLenovo });

// soluis nya dengan menggukan key unik

const laptopSpread2 = {
    laptop1: { ...laptopLenovo },
    laptop2: { ...listLaptop[0] },
    laptop3: { ...listLaptop[1] }
};

const laptopSpread3 = { laptop0: { ...laptopLenovo } };

listLaptop.forEach((laptop, index) => {
    laptopSpread[`laptop${index + 1}`] = { ...laptop };
});

const laptopSpread4 = {};

[listLaptop[0], listLaptop[1], laptopLenovo].forEach((laptop, index) => {
    Object.keys(laptop).forEach(key => {
        if (!laptopSpread4[key]) {
            laptopSpread4[key] = []; // Inisialisasi array jika belum ada
        }
        laptopSpread4[key].push(laptop[key]);
    });
});

console.log(laptopSpread4);
INFO