1

I have an array that looks likes this

[ { length: 5.6, width: 5.1, height: 5.3 }, { length: 7.7, width: 6.4, height: 6.5 } ] 

I need to reorder each object to different keys based on criteria of the largest number in each object is length, second largest width, smallest height. what would be the best way to do this? Is there a way to do a comparison inside of map function to find the largest of the 3 values and set it to length the smallest set it to height and the one left set to width?

5
  • What is the expected result? Commented Sep 3, 2020 at 20:44
  • [ { length: 5.6, width: 5.3, height: 5.1 }, { length: 7.7, width: 6.5, height: 6.4 } ] Commented Sep 3, 2020 at 20:49
  • I basically need to run a function that creates a new array where the largest number of each object is set to key: length second largest number set to key: width and smallest number set to key: height Commented Sep 3, 2020 at 20:53
  • See answer. Commented Sep 3, 2020 at 20:54
  • Try this: stackoverflow.com/questions/13820759/… Commented Sep 3, 2020 at 21:03

2 Answers 2

2

Inside the map function, put them all in an array and then sort them. Then you can return a new object in the map with the order you wish.

let arr = [ { length: 5.6, width: 5.1, height: 5.3 }, { length: 7.7, width: 6.4, height: 6.5 }, { length: 2.7, width: 6.3, height: 9.5 } ]; let res = arr.map(({length, width, height}) => { let temp = [length, width, height].sort((a, b) => b - a); return { length: temp[0], width: temp[1], height: temp[2] } }); console.log(res);

EDIT:

thank you this worked perfectly once i have my new array how would I get the largest length and largest width from the entire array?

Expand the code snippet below to see the answer to your secondary question:

let arr = [ { length: 5.6, width: 5.1, height: 5.3 }, { length: 7.7, width: 6.4, height: 6.5 }, { length: 2.7, width: 6.3, height: 9.5 } ]; console.log('max length = ', Math.max(...arr.map(a => a.length))); console.log('max width = ', Math.max(...arr.map(a => a.width))); console.log('max height = ', Math.max(...arr.map(a => a.height))); console.log('min length = ', Math.min(...arr.map(a => a.length))); console.log('min width = ', Math.min(...arr.map(a => a.width))); console.log('min height = ', Math.min(...arr.map(a => a.height)));

Sign up to request clarification or add additional context in comments.

2 Comments

thank you this worked perfectly once i have my new array how would I get the largest length and largest width from the entire array?
I edited the answer above to cover your secondary question.
1

If the properties of the objects within the array are always length, width and height then yes, you can achieve this with the map function:

const arr = [ { length: 5.6, width: 5.1, height: 5.3 }, { length: 7.7, width: 6.4, height: 6.5 } ] const result = arr.map(obj => { const max = Math.max(obj.length, obj.width, obj.height) const min = Math.min(obj.length, obj.width, obj.height) // This calculation will leave us with the remainder of the 3 const mid = obj.length + obj.width + obj.height - max - min return { length: max, width: mid, height: min } }) console.log(result);

Edit: I prefer @Rahul Bhobe's cleaner approach to this: https://stackoverflow.com/a/63731483/3011431. Again, the solution works perfectly if the object will always only contain length, width and height

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.