A
A
A person from Kazakhstan2020-05-13 15:02:27
JavaScript
A person from Kazakhstan, 2020-05-13 15:02:27

How to add numbers?

There are SVGs that have a viewBox.
It is necessary to add these numbers so that the result is 0 0 97 54 .
To do this, I looped through all the svg and got all the viewBox like this:

let svg = document.querySelectorAll("svg");
svg.forEach(function (el){
  let viewbox = el.getAttribute("viewBox");
  console.log(viewbox)
})

Got this:

"0 0 33 18"
"0 0 33 9"
"0 0 31 27"


For addition I use But the error appears viewbox.reduce is not a function . How can you add these numbers to get this result? https://codepen.io/topicstarter/pen/MWaBOvB

viewbox.reduce(function (a,b){return a+b;})



Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-05-13
@LenovoId

const viewBox = Array
  .from(document.querySelectorAll('svg'), n => n.getAttribute('viewBox').split(' '))
  .reduce((acc, n) => (n.forEach((m, i) => (acc[i] = (acc[i] || 0) + +m)), acc), [])
  .join(' ');

A
Andrey Pastukhov, 2020-05-13
@tyllo

viewbox.split(' ').reduce(function (a,b){return a+b;})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question