简体   繁体   中英

JS assign array with objects values to object keys

I have an array with objects and an object. I want to use the array's every value parameters as the second object values.

here is the array:

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},
    ]

and here is the object:

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: ''
        }

So i want to make the 'has_wifi' parameter to be equal with the 'Wifi', which is true in this case. expected output:

...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,...

Thanks!

I tried these:

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;
    })

Iterate over attributes and construct corresponding key by replacing spaces with underscores.

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) 

You're just using the index of the array, not the adapted property name you want to create on the target object. Read key and value from the array and assign them in a loop to the object:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM