Answer the question
In order to leave comments, you need to log in
How to convert from array to object by element count in JS?
Good afternoon!
There is an array
['data-name="SG 553 | Integrale"',
'data-quality="Classified"',
'data-minwear="0.00"',
'data-maxwear="1.00"',
'data-name="Dual Berettas | Twin Turbo"',
'data-quality="Classified"',
'data-minwear="0.00"',
'data-maxwear="1.00"',
'data-name="AK-47 | Safety Net"',
'data-quality="Restricted"',
'data-minwear="0.00"',
'data-maxwear="0.60"']
collection = {
1: {
Quality: {
Classified: {
'SG 553 | Integrale': 0.00-1.00,
'Dual Berettas | Twin Turbo': 0.00-1.00,
}
Restricted: {
'AK-47 | Safety Net': 0.00-0,60
}
}
}
}
Answer the question
In order to leave comments, you need to log in
let collection = {1: {Quality: {}}};
for (let i = 0, l = data.length; i < l; i += 4) {
const [name, quality, minwear, maxwear] = data.slice(i, i + 4).map((n) => n.match(/"(.*?)"/)[1]);
collection[1].Quality[quality] = collection[1].Quality[quality] || {};
collection[1].Quality[quality][name] = `${minwear}-${maxwear}`;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question