name
, а значение – весь объект с name
и value
. Перезаписывать предыдущий с таким же name
или нет – по условию;const uniq = (a, b) => Object.values(
a.concat(b).reduce(
(acc, o) => {
if (!acc[o.name]) acc[o.name] = o;
else if (Number.isFinite(o.value)) acc[o.name].value = o.value;
return acc;
}, {}
)
);
//###########################
const first = [{ name: 'first', value: '' }, { name: 'second', value: 10 }];
const second = [{ name: 'first', value: 0 }, { name: 'third', value: 20 }];
/*
[
{
"name": "first",
"value": 0
},
{
"name": "second",
"value": 10
},
{
"name": "third",
"value": 20
}
]
*/
//###########################
const first = [{ name: 'first', value: 100 }, { name: 'second', value: 0 }];
const second = [{ name: 'first', value: -111 }, { name: 'second', value: '' }];
/*
[
{
"name": "first",
"value": -111
},
{
"name": "second",
"value": 0
}
]
*/
var str = `<div draggable="true" onclick="return false;" id="item_col-2" class="layout_el wtf col-2 gap">
<div class="g-grid" ondrop="drop(event, this)" ondragover="allowDrop(event)">123</div>
<div class="g-grid" ondrop="drop(event, this)" ondragover="allowDrop(event)">321</div>
</div>`;
// Допустимые классы
const allowClasses = new Set(['col-2', 'gap', 'g-grid']);
const wrapper = document.createElement('div');
wrapper.innerHTML = str;
wrapper.querySelectorAll('*').forEach(el => {
[...el.attributes].forEach(attr => {
if (attr.name !== 'class') {
el.removeAttribute(attr.name);
} else {
[...new Set(el.classList)].filter(x => !allowClasses.has(x)).forEach(cls => {
el.classList.remove(cls);
});
}
});
});
console.log(wrapper.innerHTML);
.....
<script src="script.js"></script>
</body>
</html>
var select, html;
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}
window.addEventListener('DOMContentLoaded', e=>{
select = document.querySelector('select');
html = document.querySelector('html');
document.body.style.padding = '10px';
select.onchange = function() {
( select.value === 'black' ) ? update('black','white') : update('white','black');
}
});