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

时间:2017-07-04 17:20:35

标签: javascript arrays for-loop foreach javascript-objects

我有一个包含对象和对象的数组。 我想使用数组的每个值参数作为第二个对象值。

这是数组:

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

2 个答案:

答案 0 :(得分:1)

迭代attributes并通过用下划线替换空格来构造相应的键。

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

&#13;
&#13;
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)
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您只是使用数组的索引,而不是要在目标对象上创建的自适应属性名称。从数组中读取键和值,并将它们循环分配给对象:

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