Answer the question
In order to leave comments, you need to log in
How to parse product features in div blocks and create a JavaScript array from them?
Good afternoon. There is a site with a product, for example - https://www.xcom-shop.ru/hp_22-c0015ur_645720.html
There is a code that I paste into the Google Chrome console and execute on this site:
$('.prop-line').each(function(index, element) {
if (!$(element).hasClass('prop-line-wide')) {
var xcom_prop_name = $(element).find('.call').text();
var xcom_prop_value = $.trim($(element).find('.prop-value').text());
var xcom_prop_group = $.trim($(element).find('h3').text());
if ($.trim($(element).find('h3').text()) == "") {
console.log("TITLE_PROP: " + xcom_prop_name, "PROP_VALUE: " + xcom_prop_value);
} else {
console.log("GROUP_PROPS: " + xcom_prop_group);
}
}
});
array(
[GROUP_PROPS] => array(
"НАЗВАНИЕ_ГРУППЫ_ХАР-ОК_1" =>
array(
[НАЗВАНИЕ_ХАР-КИ_1] => "ЗНАЧЕНИЕ_ХАР-КИ_1",
[НАЗВАНИЕ_ХАР-КИ_2] => "ЗНАЧЕНИЕ_ХАР-КИ_2",
[НАЗВАНИЕ_ХАР-КИ_3] => "ЗНАЧЕНИЕ_ХАР-КИ_3",
...
),
"НАЗВАНИЕ_ГРУППЫ_ХАР-ОК_2" =>
array(
[НАЗВАНИЕ_ХАР-КИ_1] => "ЗНАЧЕНИЕ_ХАР-КИ_1",
[НАЗВАНИЕ_ХАР-КИ_2] => "ЗНАЧЕНИЕ_ХАР-КИ_2",
[НАЗВАНИЕ_ХАР-КИ_3] => "ЗНАЧЕНИЕ_ХАР-КИ_3",
...
),
...
),
);
Answer the question
In order to leave comments, you need to log in
Here is the solution:
let arr = {};
$('#prop-columns .prop-line.delimiter').each(function () {
const category = $(this);
let props = {};
category.nextUntil('.delimiter').each(function() {
const item = $(this);
props[item.find('.call').text()] = item.find('.prop-value').text();
});
arr[category.text()] = props;
});
console.log(arr);
Create a Map object, new Map(string, Array). Where the key will be the name of the group, and the value will be an array of objects {key: value}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question