JS将具有对象值的数组赋值给对象键

时间:2023-01-16 11:32:19

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'参数与'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,...

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

2 个解决方案

#1


1  

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

#2


1  

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)

#1


1  

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

#2


1  

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)