简体   繁体   English

JS将带有对象值的数组分配给对象键

[英]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. 所以我想使“ has_wifi”参数等于“ Wifi”,在这种情况下是正确的。 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并通过用下划线替换空格来构造相应的键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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