Answer the question
In order to leave comments, you need to log in
How to add an array of new JavaScript elements to an array element?
Good afternoon. There is this code:
var arrProps = ["GROUP_PROPS"];
var groups = ["Тест1", "Тест2", "Тест3"];
arrProps.push(groups);
array(
[GROUP_PROPS] => array("Тест1", "Тест2", "Тест3"),
);
Answer the question
In order to leave comments, you need to log in
So?
https://www.xcom-shop.ru/hp_22-c0015ur_645720.html
all = [...document.querySelectorAll('.prop-column div')].filter(div => !div.matches('.hide, .call, .prop-value'))
obj = {}
current = ''
all.forEach(div => {
try {
if(div.matches('.delimiter')) {
obj[div.textContent] = []
current = div.textContent
} else {
obj[current].push({
[div.querySelector('.call').textContent] : div.querySelector('.prop-value').textContent
})
}
} catch {}
})
console.log(obj)
You can't put an array in a string element =) If you want to make an array an object, then it's already more complicated:
const arr = ['some'];
const props = ['something', 'more', '...'];
function createObjectFromArray(array) {
let obj = {};
array.map(itm => obj[itm] = itm);
return obj;
}
function transformArrayToObject(arr, elementIndex, value) {
if(!arr || isNaN(elementIndex) || !value) return {};
const objectFromArr = createObjectFromArray(arr);
objectFromArr[arr[elementIndex]] = value;
return objectFromArr;
}
console.log(transformArrayToObject(arr, 0, props));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question