Answer the question
In order to leave comments, you need to log in
How to pass an array as a variable to an object?
I want to shorten the entry, it works, but I just want to substitute a variable:
let arrOptions = [
{ title: 'Домашний конфитюр', price: 'null' },
{ title: 'Изюм и орехи', price: 'null' },
{ title: 'Банан и шоколад', price: 'null' },
{ title: 'Яблоко и орехи', price: 'null' }
];
//создаем объект
const newAttributes = {
name: 'Дополнительно',
visible: true,
variation: true,
};
//добавляем ключ и значение массив
newAttributes.options = arrOptions;
newProduct.attributes = [newAttributes];
//работает корректно (консоль):
{
name: 'Дополнительно',
visible: true,
variation: true,
options: [
{ title: 'Домашний конфитюр', price: 'null' },
{ title: 'Изюм и орехи', price: 'null' },
{ title: 'Банан и шоколад', price: 'null' },
{ title: 'Яблоко и орехи', price: 'null' }
]
}
const newAttributes = {
name: 'Дополнительно',
visible: true,
variation: true,
attributes: `${arrOptions}`,
};
//то результат некорректный
{
name: 'Дополнительно',
visible: true,
variation: true,
attributes: '[object Object],[object Object],[object Object],[object Object]'
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question