-4

I want to know how we can loop through and add pixel values?

Example,

let testArray = [{width: '150px'}, {width : '150px'}]

So i want to loop through it and have to return total value i.e '300px'

Can someone please help me?

6
  • Basically i need to calculate 150px + 150 px = 300 px, but i want to loop through it using map as width values may increase Commented Nov 21, 2022 at 14:01
  • You can't have the same key twice in an object Commented Nov 21, 2022 at 14:08
  • There is nothing really to loop or map since it is an array with exactly one object. Also, that object can't have a property with the same name 2 or more times. Can you explain better what is your use case? Commented Nov 21, 2022 at 14:09
  • @Marios updated the array of objects, so i want to loop through array of objects of width key and have to do addition Commented Nov 21, 2022 at 14:12
  • @adiga updated the array of obejcts Commented Nov 21, 2022 at 14:16

1 Answer 1

0
 let sumWidth = 0
    testArray.forEach((object)=>{
    sumWidth+= parseInt(object.width.slice(0,-2))
    }

let result = sumWidth.toString() + 'px'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.