簡體   English   中英

JS將帶有對象值的數組分配給對象鍵

[英]JS assign array with objects values to object keys

我有一個帶有對象和一個對象的數組。 我想將數組的每個值參數用作第二個對象值。

這是數組:

attributes = [
        {key: 'Wifi', value: true},
        {key: 'Parking', value: false},
        {key: 'Pets', value: false},
        {key: 'Restaurant', value: false},
        {key: 'Bar', value: false},
        {key: 'Swimming pool', value: false},
        {key: 'Air conditioning', value: false},
        {key: 'Gym', value: true},
    ]

這是對象:

data = {
        type: 'hotels',
        id: '11',
        attributes: {
                has_wifi: false,
                has_parking: false,
                has_pets: false,
                has_restaurant: false,
                has_bar: false,
                has_swimming_pool: false,
                has_air_conditioning: false,
                hsa_gym: false,
                name: '',
                main_image_src: 'https://placebear.com/300/300',
                meal_plan: '',
                user_id: '1',
                booking_id: '1',
                amount: '5000',
                currency: 'HUF',
                status: 'pending',
                stars: ''
        }

所以我想使“ has_wifi”參數等於“ Wifi”,在這種情況下是正確的。 預期輸出:

...has_wifi: false,
                has_parking: true,
                has_pets: false,
                has_restaurant: false,
                has_bar: false,
                has_swimming_pool: false,
                has_air_conditioning: false,
                hsa_gym: true,...

謝謝!

我嘗試了這些:

for (let i = 0; i < 8; i++) {
         this.hotelservice.hotel.data.attributes[i] = this.hotelAttributes.data.attributes[i].value;
    }

this.hotelAttributes.data.attributes.forEach ((attr, index) => {
        this.hotelservice.hotel.data.attributes[index] = attr.value;
    })

遍歷attributes並通過用下划線替換空格來構造相應的鍵。

attributes.forEach(({key, value}) => {
    data.attributes[`has_${key.toLowerCase().replace(/\s+/g,'_')}`] = value
})

 attributes = [ {key: 'Wifi', value: true}, {key: 'Parking', value: false}, {key: 'Pets', value: false}, {key: 'Restaurant', value: false}, {key: 'Bar', value: false}, {key: 'Swimming pool', value: false}, {key: 'Air conditioning', value: false}, {key: 'Gym', value: true}, ] data = { type: 'hotels', id: '11', attributes: { has_wifi: false, has_parking: false, has_pets: false, has_restaurant: false, has_bar: false, has_swimming_pool: false, has_air_conditioning: false, hsa_gym: false, name: '', main_image_src: 'https://placebear.com/300/300', meal_plan: '', user_id: '1', booking_id: '1', amount: '5000', currency: 'HUF', status: 'pending', stars: '' } } attributes.forEach(({key, value}) => { data.attributes[`has_${key.toLowerCase().replace(/\\s+/g,'_')}`] = value }) console.log(data.attributes) 

您僅使用數組的索引,而不是要在目標對象上創建的適應屬性名稱。 從數組中讀取鍵和值,並在循環中將它們分配給對象:

function keyFormat(k) {
    return 'has_'+k.toLowerCase().replace(/ /g, '_');
}

for (var {key, value} of this.hotelAttributes.data.attributes)
    this.hotelservice.hotel.data.attributes[keyFormat(key)] = value;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM