S
S
Speakea1y12892019-07-04 13:11:52
JavaScript
Speakea1y1289, 2019-07-04 13:11:52

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

Task: add to the arrProps array, to its ["GROUP_PROPS"] element, another array groups, so that the array structure is as follows:
array(
  [GROUP_PROPS] => array("Тест1", "Тест2", "Тест3"),
);

How to do it? How to access the GROUP_PROPS element?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Suha, 2019-07-04
@andreysuha

you here

H
hzzzzl, 2019-07-04
@hzzzzl

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)

R
Roman Chasovitin, 2019-07-04
@RomanChasovitin

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 question

Ask a Question

731 491 924 answers to any question